1 //===--------------------------- DwarfParser.hpp --------------------------===//
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 {
73 kRegisterOffsetFromCFA,
75 kRegisterAtExpression,
78 struct RegisterLocation {
79 RegisterSavedWhere location;
80 bool initialStateSaved;
83 /// Information about a frame layout and registers saved determined
84 /// by "running" the DWARF FDE "instructions"
87 int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset
88 int64_t cfaExpression; // CFA = expression
89 uint32_t spExtraArgSize;
90 RegisterLocation savedRegisters[kMaxRegisterNumber + 1];
91 enum class InitializeTime { kLazy, kNormal };
93 // When saving registers, this data structure is lazily initialized.
94 PrologInfo(InitializeTime IT = InitializeTime::kNormal) {
95 if (IT == InitializeTime::kNormal)
96 memset(this, 0, sizeof(*this));
98 void checkSaveRegister(uint64_t reg, PrologInfo &initialState) {
99 if (!savedRegisters[reg].initialStateSaved) {
100 initialState.savedRegisters[reg] = savedRegisters[reg];
101 savedRegisters[reg].initialStateSaved = true;
104 void setRegister(uint64_t reg, RegisterSavedWhere newLocation,
105 int64_t newValue, PrologInfo &initialState) {
106 checkSaveRegister(reg, initialState);
107 savedRegisters[reg].location = newLocation;
108 savedRegisters[reg].value = newValue;
110 void setRegisterLocation(uint64_t reg, RegisterSavedWhere newLocation,
111 PrologInfo &initialState) {
112 checkSaveRegister(reg, initialState);
113 savedRegisters[reg].location = newLocation;
115 void setRegisterValue(uint64_t reg, int64_t newValue,
116 PrologInfo &initialState) {
117 checkSaveRegister(reg, initialState);
118 savedRegisters[reg].value = newValue;
120 void restoreRegisterToInitialState(uint64_t reg, PrologInfo &initialState) {
121 if (savedRegisters[reg].initialStateSaved)
122 savedRegisters[reg] = initialState.savedRegisters[reg];
123 // else the register still holds its initial state
127 struct PrologInfoStackEntry {
128 PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
129 : next(n), info(i) {}
130 PrologInfoStackEntry *next;
134 struct RememberStack {
135 PrologInfoStackEntry *entry;
136 RememberStack() : entry(nullptr) {}
138 #if defined(_LIBUNWIND_REMEMBER_CLEANUP_NEEDED)
139 // Clean up rememberStack. Even in the case where every
140 // DW_CFA_remember_state is paired with a DW_CFA_restore_state,
141 // parseInstructions can skip restore opcodes if it reaches the target PC
142 // and stops interpreting, so we have to make sure we don't leak memory.
144 PrologInfoStackEntry *next = entry->next;
145 _LIBUNWIND_REMEMBER_FREE(entry);
152 static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
153 uintptr_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
155 static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
156 FDE_Info *fdeInfo, CIE_Info *cieInfo);
157 static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
158 const CIE_Info &cieInfo, pint_t upToPC,
159 int arch, PrologInfo *results);
161 static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
164 /// Parse a FDE into a CIE_Info and an FDE_Info
165 template <typename A>
166 const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
167 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
169 pint_t cfiLength = (pint_t)addressSpace.get32(p);
171 if (cfiLength == 0xffffffff) {
172 // 0xffffffff means length is really next 8 bytes
173 cfiLength = (pint_t)addressSpace.get64(p);
177 return "FDE has zero length"; // zero terminator
178 uint32_t ciePointer = addressSpace.get32(p);
180 return "FDE is really a CIE"; // this is a CIE not an FDE
181 pint_t nextCFI = p + cfiLength;
182 pint_t cieStart = p - ciePointer;
183 const char *err = parseCIE(addressSpace, cieStart, cieInfo);
187 // Parse pc begin and range.
189 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
191 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
192 // Parse rest of info.
194 // Check for augmentation length.
195 if (cieInfo->fdesHaveAugmentationData) {
196 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
197 pint_t endOfAug = p + augLen;
198 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
199 // Peek at value (without indirection). Zero means no LSDA.
200 pint_t lsdaStart = p;
201 if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
203 // Reset pointer and re-parse LSDA address.
206 addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
211 fdeInfo->fdeStart = fdeStart;
212 fdeInfo->fdeLength = nextCFI - fdeStart;
213 fdeInfo->fdeInstructions = p;
214 fdeInfo->pcStart = pcStart;
215 fdeInfo->pcEnd = pcStart + pcRange;
216 return NULL; // success
219 /// Scan an eh_frame section to find an FDE for a pc
220 template <typename A>
221 bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
222 uintptr_t sectionLength, pint_t fdeHint,
223 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
224 //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
225 pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
226 const pint_t ehSectionEnd = (sectionLength == UINTPTR_MAX)
227 ? static_cast<pint_t>(-1)
228 : (ehSectionStart + sectionLength);
229 while (p < ehSectionEnd) {
230 pint_t currentCFI = p;
231 //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
232 pint_t cfiLength = addressSpace.get32(p);
234 if (cfiLength == 0xffffffff) {
235 // 0xffffffff means length is really next 8 bytes
236 cfiLength = (pint_t)addressSpace.get64(p);
240 return false; // zero terminator
241 uint32_t id = addressSpace.get32(p);
246 // Process FDE to see if it covers pc.
247 pint_t nextCFI = p + cfiLength;
248 uint32_t ciePointer = addressSpace.get32(p);
249 pint_t cieStart = p - ciePointer;
250 // Validate pointer to CIE is within section.
251 if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
252 if (parseCIE(addressSpace, cieStart, cieInfo) == NULL) {
254 // Parse pc begin and range.
256 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
257 pint_t pcRange = addressSpace.getEncodedP(
258 p, nextCFI, cieInfo->pointerEncoding & 0x0F);
259 // Test if pc is within the function this FDE covers.
260 if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
261 // parse rest of info
263 // check for augmentation length
264 if (cieInfo->fdesHaveAugmentationData) {
265 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
266 pint_t endOfAug = p + augLen;
267 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
268 // Peek at value (without indirection). Zero means no LSDA.
269 pint_t lsdaStart = p;
270 if (addressSpace.getEncodedP(
271 p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
272 // Reset pointer and re-parse LSDA address.
274 fdeInfo->lsda = addressSpace
275 .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
280 fdeInfo->fdeStart = currentCFI;
281 fdeInfo->fdeLength = nextCFI - currentCFI;
282 fdeInfo->fdeInstructions = p;
283 fdeInfo->pcStart = pcStart;
284 fdeInfo->pcEnd = pcStart + pcRange;
287 // pc is not in begin/range, skip this FDE
290 // Malformed CIE, now augmentation describing pc range encoding.
293 // malformed FDE. CIE is bad
301 /// Extract info from a CIE
302 template <typename A>
303 const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
305 cieInfo->pointerEncoding = 0;
306 cieInfo->lsdaEncoding = DW_EH_PE_omit;
307 cieInfo->personalityEncoding = 0;
308 cieInfo->personalityOffsetInCIE = 0;
309 cieInfo->personality = 0;
310 cieInfo->codeAlignFactor = 0;
311 cieInfo->dataAlignFactor = 0;
312 cieInfo->isSignalFrame = false;
313 cieInfo->fdesHaveAugmentationData = false;
314 #if defined(_LIBUNWIND_TARGET_AARCH64)
315 cieInfo->addressesSignedWithBKey = false;
317 cieInfo->cieStart = cie;
319 pint_t cieLength = (pint_t)addressSpace.get32(p);
321 pint_t cieContentEnd = p + cieLength;
322 if (cieLength == 0xffffffff) {
323 // 0xffffffff means length is really next 8 bytes
324 cieLength = (pint_t)addressSpace.get64(p);
326 cieContentEnd = p + cieLength;
330 // CIE ID is always 0
331 if (addressSpace.get32(p) != 0)
332 return "CIE ID is not zero";
334 // Version is always 1 or 3
335 uint8_t version = addressSpace.get8(p);
336 if ((version != 1) && (version != 3))
337 return "CIE version is not 1 or 3";
339 // save start of augmentation string and find end
341 while (addressSpace.get8(p) != 0)
344 // parse code aligment factor
345 cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
346 // parse data alignment factor
347 cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
348 // parse return address register
349 uint64_t raReg = (version == 1) ? addressSpace.get8(p++)
350 : addressSpace.getULEB128(p, cieContentEnd);
351 assert(raReg < 255 && "return address register too large");
352 cieInfo->returnAddressRegister = (uint8_t)raReg;
353 // parse augmentation data based on augmentation string
354 const char *result = NULL;
355 if (addressSpace.get8(strStart) == 'z') {
356 // parse augmentation data length
357 addressSpace.getULEB128(p, cieContentEnd);
358 for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
359 switch (addressSpace.get8(s)) {
361 cieInfo->fdesHaveAugmentationData = true;
364 cieInfo->personalityEncoding = addressSpace.get8(p);
366 cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
367 cieInfo->personality = addressSpace
368 .getEncodedP(p, cieContentEnd, cieInfo->personalityEncoding);
371 cieInfo->lsdaEncoding = addressSpace.get8(p);
375 cieInfo->pointerEncoding = addressSpace.get8(p);
379 cieInfo->isSignalFrame = true;
381 #if defined(_LIBUNWIND_TARGET_AARCH64)
383 cieInfo->addressesSignedWithBKey = true;
387 // ignore unknown letters
392 cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
393 cieInfo->cieInstructions = p;
398 /// "run" the DWARF instructions and create the abstact PrologInfo for an FDE
399 template <typename A>
400 bool CFI_Parser<A>::parseFDEInstructions(A &addressSpace,
401 const FDE_Info &fdeInfo,
402 const CIE_Info &cieInfo, pint_t upToPC,
403 int arch, PrologInfo *results) {
404 // Alloca is used for the allocation of the rememberStack entries. It removes
405 // the dependency on new/malloc but the below for loop can not be refactored
406 // into functions. Entry could be saved during the processing of a CIE and
407 // restored by an FDE.
408 RememberStack rememberStack;
412 pint_t instructionsEnd;
416 ParseInfo parseInfoArray[] = {
417 {cieInfo.cieInstructions, cieInfo.cieStart + cieInfo.cieLength,
419 {fdeInfo.fdeInstructions, fdeInfo.fdeStart + fdeInfo.fdeLength,
420 upToPC - fdeInfo.pcStart}};
422 for (const auto &info : parseInfoArray) {
423 pint_t p = info.instructions;
424 pint_t instructionsEnd = info.instructionsEnd;
425 pint_t pcoffset = info.pcoffset;
426 pint_t codeOffset = 0;
428 // initialState initialized as registers in results are modified. Use
429 // PrologInfo accessor functions to avoid reading uninitialized data.
430 PrologInfo initialState(PrologInfo::InitializeTime::kLazy);
432 _LIBUNWIND_TRACE_DWARF("parseFDEInstructions(instructions=0x%0" PRIx64
434 static_cast<uint64_t>(instructionsEnd));
436 // see DWARF Spec, section 6.4.2 for details on unwind opcodes
437 while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
442 uint8_t opcode = addressSpace.get8(p);
448 _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
451 codeOffset = addressSpace.getEncodedP(p, instructionsEnd,
452 cieInfo.pointerEncoding);
453 _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
455 case DW_CFA_advance_loc1:
456 codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
458 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
459 static_cast<uint64_t>(codeOffset));
461 case DW_CFA_advance_loc2:
462 codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
464 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
465 static_cast<uint64_t>(codeOffset));
467 case DW_CFA_advance_loc4:
468 codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
470 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
471 static_cast<uint64_t>(codeOffset));
473 case DW_CFA_offset_extended:
474 reg = addressSpace.getULEB128(p, instructionsEnd);
475 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
476 cieInfo.dataAlignFactor;
477 if (reg > kMaxRegisterNumber) {
479 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
482 results->setRegister(reg, kRegisterInCFA, offset, initialState);
483 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
484 "offset=%" PRId64 ")\n",
487 case DW_CFA_restore_extended:
488 reg = addressSpace.getULEB128(p, instructionsEnd);
489 if (reg > kMaxRegisterNumber) {
491 "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
494 results->restoreRegisterToInitialState(reg, initialState);
495 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n",
498 case DW_CFA_undefined:
499 reg = addressSpace.getULEB128(p, instructionsEnd);
500 if (reg > kMaxRegisterNumber) {
502 "malformed DW_CFA_undefined DWARF unwind, reg too big");
505 results->setRegisterLocation(reg, kRegisterUnused, initialState);
506 _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
508 case DW_CFA_same_value:
509 reg = addressSpace.getULEB128(p, instructionsEnd);
510 if (reg > kMaxRegisterNumber) {
512 "malformed DW_CFA_same_value DWARF unwind, reg too big");
515 // <rdar://problem/8456377> DW_CFA_same_value unsupported
516 // "same value" means register was stored in frame, but its current
517 // value has not changed, so no need to restore from frame.
518 // We model this as if the register was never saved.
519 results->setRegisterLocation(reg, kRegisterUnused, initialState);
520 _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
522 case DW_CFA_register:
523 reg = addressSpace.getULEB128(p, instructionsEnd);
524 reg2 = addressSpace.getULEB128(p, instructionsEnd);
525 if (reg > kMaxRegisterNumber) {
527 "malformed DW_CFA_register DWARF unwind, reg too big");
530 if (reg2 > kMaxRegisterNumber) {
532 "malformed DW_CFA_register DWARF unwind, reg2 too big");
535 results->setRegister(reg, kRegisterInRegister, (int64_t)reg2,
537 _LIBUNWIND_TRACE_DWARF(
538 "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
540 case DW_CFA_remember_state: {
541 // Avoid operator new because that would be an upward dependency.
542 // Avoid malloc because it needs heap allocation.
543 PrologInfoStackEntry *entry =
544 (PrologInfoStackEntry *)_LIBUNWIND_REMEMBER_ALLOC(
545 sizeof(PrologInfoStackEntry));
547 entry->next = rememberStack.entry;
548 entry->info = *results;
549 rememberStack.entry = entry;
553 _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
556 case DW_CFA_restore_state:
557 if (rememberStack.entry != NULL) {
558 PrologInfoStackEntry *top = rememberStack.entry;
559 *results = top->info;
560 rememberStack.entry = top->next;
561 _LIBUNWIND_REMEMBER_FREE(top);
565 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
568 reg = addressSpace.getULEB128(p, instructionsEnd);
569 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
570 if (reg > kMaxRegisterNumber) {
571 _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
574 results->cfaRegister = (uint32_t)reg;
575 results->cfaRegisterOffset = (int32_t)offset;
576 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64
580 case DW_CFA_def_cfa_register:
581 reg = addressSpace.getULEB128(p, instructionsEnd);
582 if (reg > kMaxRegisterNumber) {
584 "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
587 results->cfaRegister = (uint32_t)reg;
588 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
590 case DW_CFA_def_cfa_offset:
591 results->cfaRegisterOffset =
592 (int32_t)addressSpace.getULEB128(p, instructionsEnd);
593 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
594 results->cfaRegisterOffset);
596 case DW_CFA_def_cfa_expression:
597 results->cfaRegister = 0;
598 results->cfaExpression = (int64_t)p;
599 length = addressSpace.getULEB128(p, instructionsEnd);
600 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
601 p += static_cast<pint_t>(length);
602 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
603 ", length=%" PRIu64 ")\n",
604 results->cfaExpression, length);
606 case DW_CFA_expression:
607 reg = addressSpace.getULEB128(p, instructionsEnd);
608 if (reg > kMaxRegisterNumber) {
610 "malformed DW_CFA_expression DWARF unwind, reg too big");
613 results->setRegister(reg, kRegisterAtExpression, (int64_t)p,
615 length = addressSpace.getULEB128(p, instructionsEnd);
616 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
617 p += static_cast<pint_t>(length);
618 _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
619 "expression=0x%" PRIx64 ", "
620 "length=%" PRIu64 ")\n",
621 reg, results->savedRegisters[reg].value, length);
623 case DW_CFA_offset_extended_sf:
624 reg = addressSpace.getULEB128(p, instructionsEnd);
625 if (reg > kMaxRegisterNumber) {
627 "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
630 offset = addressSpace.getSLEB128(p, instructionsEnd) *
631 cieInfo.dataAlignFactor;
632 results->setRegister(reg, kRegisterInCFA, offset, initialState);
633 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
634 "offset=%" PRId64 ")\n",
637 case DW_CFA_def_cfa_sf:
638 reg = addressSpace.getULEB128(p, instructionsEnd);
639 offset = addressSpace.getSLEB128(p, instructionsEnd) *
640 cieInfo.dataAlignFactor;
641 if (reg > kMaxRegisterNumber) {
643 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
646 results->cfaRegister = (uint32_t)reg;
647 results->cfaRegisterOffset = (int32_t)offset;
648 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
649 "offset=%" PRId64 ")\n",
652 case DW_CFA_def_cfa_offset_sf:
653 results->cfaRegisterOffset =
654 (int32_t)(addressSpace.getSLEB128(p, instructionsEnd) *
655 cieInfo.dataAlignFactor);
656 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
657 results->cfaRegisterOffset);
659 case DW_CFA_val_offset:
660 reg = addressSpace.getULEB128(p, instructionsEnd);
661 if (reg > kMaxRegisterNumber) {
663 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
668 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
669 cieInfo.dataAlignFactor;
670 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
671 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
672 "offset=%" PRId64 "\n",
675 case DW_CFA_val_offset_sf:
676 reg = addressSpace.getULEB128(p, instructionsEnd);
677 if (reg > kMaxRegisterNumber) {
679 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
682 offset = addressSpace.getSLEB128(p, instructionsEnd) *
683 cieInfo.dataAlignFactor;
684 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
685 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
686 "offset=%" PRId64 "\n",
689 case DW_CFA_val_expression:
690 reg = addressSpace.getULEB128(p, instructionsEnd);
691 if (reg > kMaxRegisterNumber) {
693 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
696 results->setRegister(reg, kRegisterIsExpression, (int64_t)p,
698 length = addressSpace.getULEB128(p, instructionsEnd);
699 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
700 p += static_cast<pint_t>(length);
701 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
702 "expression=0x%" PRIx64 ", length=%" PRIu64
704 reg, results->savedRegisters[reg].value, length);
706 case DW_CFA_GNU_args_size:
707 length = addressSpace.getULEB128(p, instructionsEnd);
708 results->spExtraArgSize = (uint32_t)length;
709 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
711 case DW_CFA_GNU_negative_offset_extended:
712 reg = addressSpace.getULEB128(p, instructionsEnd);
713 if (reg > kMaxRegisterNumber) {
714 _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
715 "unwind, reg too big");
718 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
719 cieInfo.dataAlignFactor;
720 results->setRegister(reg, kRegisterInCFA, -offset, initialState);
721 _LIBUNWIND_TRACE_DWARF(
722 "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
725 #if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC)
726 // The same constant is used to represent different instructions on
727 // AArch64 (negate_ra_state) and SPARC (window_save).
728 static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
729 "uses the same constant");
730 case DW_CFA_AARCH64_negate_ra_state:
732 #if defined(_LIBUNWIND_TARGET_AARCH64)
733 case REGISTERS_ARM64: {
735 results->savedRegisters[UNW_ARM64_RA_SIGN_STATE].value ^ 0x1;
736 results->setRegisterValue(UNW_ARM64_RA_SIGN_STATE, value,
738 _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
742 #if defined(_LIBUNWIND_TARGET_SPARC)
743 // case DW_CFA_GNU_window_save:
744 case REGISTERS_SPARC:
745 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
746 for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
747 results->setRegister(reg, kRegisterInRegister,
748 ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0,
752 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
753 results->setRegister(reg, kRegisterInCFA,
754 ((int64_t)reg - UNW_SPARC_L0) * 4,
766 operand = opcode & 0x3F;
767 switch (opcode & 0xC0) {
770 if (reg > kMaxRegisterNumber) {
771 _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
776 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
777 cieInfo.dataAlignFactor;
778 results->setRegister(reg, kRegisterInCFA, offset, initialState);
779 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
782 case DW_CFA_advance_loc:
783 codeOffset += operand * cieInfo.codeAlignFactor;
784 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
785 static_cast<uint64_t>(codeOffset));
789 if (reg > kMaxRegisterNumber) {
791 "malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
796 results->restoreRegisterToInitialState(reg, initialState);
797 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
798 static_cast<uint64_t>(operand));
801 _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
810 } // namespace libunwind
812 #endif // __DWARF_PARSER_HPP__