1 //===--------------------------- DwarfParser.hpp --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
9 // Parses DWARF CFIs (FDEs and CIEs).
11 //===----------------------------------------------------------------------===//
13 #ifndef __DWARF_PARSER_HPP__
14 #define __DWARF_PARSER_HPP__
21 #include "libunwind.h"
23 #include "Registers.hpp"
29 /// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
30 /// See DWARF Spec for details:
31 /// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
36 typedef typename A::pint_t pint_t;
38 /// Information encoded in a CIE (Common Information Entry)
42 pint_t cieInstructions;
43 uint8_t pointerEncoding;
45 uint8_t personalityEncoding;
46 uint8_t personalityOffsetInCIE;
48 uint32_t codeAlignFactor;
51 bool fdesHaveAugmentationData;
52 uint8_t returnAddressRegister;
53 #if defined(_LIBUNWIND_TARGET_AARCH64)
54 bool addressesSignedWithBKey;
58 /// Information about an FDE (Frame Description Entry)
62 pint_t fdeInstructions;
69 kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
71 enum RegisterSavedWhere {
74 kRegisterOffsetFromCFA,
76 kRegisterAtExpression,
79 struct RegisterLocation {
80 RegisterSavedWhere location;
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 uint32_t codeOffsetAtStackDecrement;
91 bool registersInOtherRegisters;
93 RegisterLocation savedRegisters[kMaxRegisterNumber + 1];
96 struct PrologInfoStackEntry {
97 PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
99 PrologInfoStackEntry *next;
103 static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
104 uint32_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
106 static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
107 FDE_Info *fdeInfo, CIE_Info *cieInfo);
108 static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
109 const CIE_Info &cieInfo, pint_t upToPC,
110 int arch, PrologInfo *results);
112 static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
115 static bool parseInstructions(A &addressSpace, pint_t instructions,
116 pint_t instructionsEnd, const CIE_Info &cieInfo,
118 PrologInfoStackEntry *&rememberStack, int arch,
119 PrologInfo *results);
122 /// Parse a FDE into a CIE_Info and an FDE_Info
123 template <typename A>
124 const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
125 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
127 pint_t cfiLength = (pint_t)addressSpace.get32(p);
129 if (cfiLength == 0xffffffff) {
130 // 0xffffffff means length is really next 8 bytes
131 cfiLength = (pint_t)addressSpace.get64(p);
135 return "FDE has zero length"; // end marker
136 uint32_t ciePointer = addressSpace.get32(p);
138 return "FDE is really a CIE"; // this is a CIE not an FDE
139 pint_t nextCFI = p + cfiLength;
140 pint_t cieStart = p - ciePointer;
141 const char *err = parseCIE(addressSpace, cieStart, cieInfo);
145 // Parse pc begin and range.
147 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
149 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
150 // Parse rest of info.
152 // Check for augmentation length.
153 if (cieInfo->fdesHaveAugmentationData) {
154 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
155 pint_t endOfAug = p + augLen;
156 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
157 // Peek at value (without indirection). Zero means no LSDA.
158 pint_t lsdaStart = p;
159 if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
161 // Reset pointer and re-parse LSDA address.
164 addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
169 fdeInfo->fdeStart = fdeStart;
170 fdeInfo->fdeLength = nextCFI - fdeStart;
171 fdeInfo->fdeInstructions = p;
172 fdeInfo->pcStart = pcStart;
173 fdeInfo->pcEnd = pcStart + pcRange;
174 return NULL; // success
177 /// Scan an eh_frame section to find an FDE for a pc
178 template <typename A>
179 bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
180 uint32_t sectionLength, pint_t fdeHint,
181 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
182 //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
183 pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
184 const pint_t ehSectionEnd = p + sectionLength;
185 while (p < ehSectionEnd) {
186 pint_t currentCFI = p;
187 //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
188 pint_t cfiLength = addressSpace.get32(p);
190 if (cfiLength == 0xffffffff) {
191 // 0xffffffff means length is really next 8 bytes
192 cfiLength = (pint_t)addressSpace.get64(p);
196 return false; // end marker
197 uint32_t id = addressSpace.get32(p);
202 // Process FDE to see if it covers pc.
203 pint_t nextCFI = p + cfiLength;
204 uint32_t ciePointer = addressSpace.get32(p);
205 pint_t cieStart = p - ciePointer;
206 // Validate pointer to CIE is within section.
207 if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
208 if (parseCIE(addressSpace, cieStart, cieInfo) == NULL) {
210 // Parse pc begin and range.
212 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
213 pint_t pcRange = addressSpace.getEncodedP(
214 p, nextCFI, cieInfo->pointerEncoding & 0x0F);
215 // Test if pc is within the function this FDE covers.
216 if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
217 // parse rest of info
219 // check for augmentation length
220 if (cieInfo->fdesHaveAugmentationData) {
221 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
222 pint_t endOfAug = p + augLen;
223 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
224 // Peek at value (without indirection). Zero means no LSDA.
225 pint_t lsdaStart = p;
226 if (addressSpace.getEncodedP(
227 p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
228 // Reset pointer and re-parse LSDA address.
230 fdeInfo->lsda = addressSpace
231 .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
236 fdeInfo->fdeStart = currentCFI;
237 fdeInfo->fdeLength = nextCFI - currentCFI;
238 fdeInfo->fdeInstructions = p;
239 fdeInfo->pcStart = pcStart;
240 fdeInfo->pcEnd = pcStart + pcRange;
243 // pc is not in begin/range, skip this FDE
246 // Malformed CIE, now augmentation describing pc range encoding.
249 // malformed FDE. CIE is bad
257 /// Extract info from a CIE
258 template <typename A>
259 const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
261 cieInfo->pointerEncoding = 0;
262 cieInfo->lsdaEncoding = DW_EH_PE_omit;
263 cieInfo->personalityEncoding = 0;
264 cieInfo->personalityOffsetInCIE = 0;
265 cieInfo->personality = 0;
266 cieInfo->codeAlignFactor = 0;
267 cieInfo->dataAlignFactor = 0;
268 cieInfo->isSignalFrame = false;
269 cieInfo->fdesHaveAugmentationData = false;
270 #if defined(_LIBUNWIND_TARGET_AARCH64)
271 cieInfo->addressesSignedWithBKey = false;
273 cieInfo->cieStart = cie;
275 pint_t cieLength = (pint_t)addressSpace.get32(p);
277 pint_t cieContentEnd = p + cieLength;
278 if (cieLength == 0xffffffff) {
279 // 0xffffffff means length is really next 8 bytes
280 cieLength = (pint_t)addressSpace.get64(p);
282 cieContentEnd = p + cieLength;
286 // CIE ID is always 0
287 if (addressSpace.get32(p) != 0)
288 return "CIE ID is not zero";
290 // Version is always 1 or 3
291 uint8_t version = addressSpace.get8(p);
292 if ((version != 1) && (version != 3))
293 return "CIE version is not 1 or 3";
295 // save start of augmentation string and find end
297 while (addressSpace.get8(p) != 0)
300 // parse code aligment factor
301 cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
302 // parse data alignment factor
303 cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
304 // parse return address register
305 uint64_t raReg = addressSpace.getULEB128(p, cieContentEnd);
306 assert(raReg < 255 && "return address register too large");
307 cieInfo->returnAddressRegister = (uint8_t)raReg;
308 // parse augmentation data based on augmentation string
309 const char *result = NULL;
310 if (addressSpace.get8(strStart) == 'z') {
311 // parse augmentation data length
312 addressSpace.getULEB128(p, cieContentEnd);
313 for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
314 switch (addressSpace.get8(s)) {
316 cieInfo->fdesHaveAugmentationData = true;
319 cieInfo->personalityEncoding = addressSpace.get8(p);
321 cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
322 cieInfo->personality = addressSpace
323 .getEncodedP(p, cieContentEnd, cieInfo->personalityEncoding);
326 cieInfo->lsdaEncoding = addressSpace.get8(p);
330 cieInfo->pointerEncoding = addressSpace.get8(p);
334 cieInfo->isSignalFrame = true;
336 #if defined(_LIBUNWIND_TARGET_AARCH64)
338 cieInfo->addressesSignedWithBKey = true;
342 // ignore unknown letters
347 cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
348 cieInfo->cieInstructions = p;
353 /// "run" the DWARF instructions and create the abstact PrologInfo for an FDE
354 template <typename A>
355 bool CFI_Parser<A>::parseFDEInstructions(A &addressSpace,
356 const FDE_Info &fdeInfo,
357 const CIE_Info &cieInfo, pint_t upToPC,
358 int arch, PrologInfo *results) {
360 memset(results, '\0', sizeof(PrologInfo));
361 PrologInfoStackEntry *rememberStack = NULL;
363 // parse CIE then FDE instructions
364 return parseInstructions(addressSpace, cieInfo.cieInstructions,
365 cieInfo.cieStart + cieInfo.cieLength, cieInfo,
366 (pint_t)(-1), rememberStack, arch, results) &&
367 parseInstructions(addressSpace, fdeInfo.fdeInstructions,
368 fdeInfo.fdeStart + fdeInfo.fdeLength, cieInfo,
369 upToPC - fdeInfo.pcStart, rememberStack, arch,
373 /// "run" the DWARF instructions
374 template <typename A>
375 bool CFI_Parser<A>::parseInstructions(A &addressSpace, pint_t instructions,
376 pint_t instructionsEnd,
377 const CIE_Info &cieInfo, pint_t pcoffset,
378 PrologInfoStackEntry *&rememberStack,
379 int arch, PrologInfo *results) {
380 pint_t p = instructions;
381 pint_t codeOffset = 0;
382 PrologInfo initialState = *results;
384 _LIBUNWIND_TRACE_DWARF("parseInstructions(instructions=0x%0" PRIx64 ")\n",
385 static_cast<uint64_t>(instructionsEnd));
387 // see DWARF Spec, section 6.4.2 for details on unwind opcodes
388 while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
393 uint8_t opcode = addressSpace.get8(p);
395 #if !defined(_LIBUNWIND_NO_HEAP)
396 PrologInfoStackEntry *entry;
401 _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
405 addressSpace.getEncodedP(p, instructionsEnd, cieInfo.pointerEncoding);
406 _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
408 case DW_CFA_advance_loc1:
409 codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
411 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
412 static_cast<uint64_t>(codeOffset));
414 case DW_CFA_advance_loc2:
415 codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
417 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
418 static_cast<uint64_t>(codeOffset));
420 case DW_CFA_advance_loc4:
421 codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
423 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
424 static_cast<uint64_t>(codeOffset));
426 case DW_CFA_offset_extended:
427 reg = addressSpace.getULEB128(p, instructionsEnd);
428 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
429 * cieInfo.dataAlignFactor;
430 if (reg > kMaxRegisterNumber) {
432 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
435 results->savedRegisters[reg].location = kRegisterInCFA;
436 results->savedRegisters[reg].value = offset;
437 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
438 "offset=%" PRId64 ")\n",
441 case DW_CFA_restore_extended:
442 reg = addressSpace.getULEB128(p, instructionsEnd);
443 if (reg > kMaxRegisterNumber) {
445 "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
448 results->savedRegisters[reg] = initialState.savedRegisters[reg];
449 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n", reg);
451 case DW_CFA_undefined:
452 reg = addressSpace.getULEB128(p, instructionsEnd);
453 if (reg > kMaxRegisterNumber) {
455 "malformed DW_CFA_undefined DWARF unwind, reg too big");
458 results->savedRegisters[reg].location = kRegisterUnused;
459 _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
461 case DW_CFA_same_value:
462 reg = addressSpace.getULEB128(p, instructionsEnd);
463 if (reg > kMaxRegisterNumber) {
465 "malformed DW_CFA_same_value DWARF unwind, reg too big");
468 // <rdar://problem/8456377> DW_CFA_same_value unsupported
469 // "same value" means register was stored in frame, but its current
470 // value has not changed, so no need to restore from frame.
471 // We model this as if the register was never saved.
472 results->savedRegisters[reg].location = kRegisterUnused;
473 // set flag to disable conversion to compact unwind
474 results->sameValueUsed = true;
475 _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
477 case DW_CFA_register:
478 reg = addressSpace.getULEB128(p, instructionsEnd);
479 reg2 = addressSpace.getULEB128(p, instructionsEnd);
480 if (reg > kMaxRegisterNumber) {
482 "malformed DW_CFA_register DWARF unwind, reg too big");
485 if (reg2 > kMaxRegisterNumber) {
487 "malformed DW_CFA_register DWARF unwind, reg2 too big");
490 results->savedRegisters[reg].location = kRegisterInRegister;
491 results->savedRegisters[reg].value = (int64_t)reg2;
492 // set flag to disable conversion to compact unwind
493 results->registersInOtherRegisters = true;
494 _LIBUNWIND_TRACE_DWARF(
495 "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
497 #if !defined(_LIBUNWIND_NO_HEAP)
498 case DW_CFA_remember_state:
499 // avoid operator new, because that would be an upward dependency
500 entry = (PrologInfoStackEntry *)malloc(sizeof(PrologInfoStackEntry));
502 entry->next = rememberStack;
503 entry->info = *results;
504 rememberStack = entry;
508 _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
510 case DW_CFA_restore_state:
511 if (rememberStack != NULL) {
512 PrologInfoStackEntry *top = rememberStack;
513 *results = top->info;
514 rememberStack = top->next;
519 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
523 reg = addressSpace.getULEB128(p, instructionsEnd);
524 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
525 if (reg > kMaxRegisterNumber) {
526 _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
529 results->cfaRegister = (uint32_t)reg;
530 results->cfaRegisterOffset = (int32_t)offset;
531 _LIBUNWIND_TRACE_DWARF(
532 "DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64 ")\n", reg, offset);
534 case DW_CFA_def_cfa_register:
535 reg = addressSpace.getULEB128(p, instructionsEnd);
536 if (reg > kMaxRegisterNumber) {
538 "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
541 results->cfaRegister = (uint32_t)reg;
542 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
544 case DW_CFA_def_cfa_offset:
545 results->cfaRegisterOffset = (int32_t)
546 addressSpace.getULEB128(p, instructionsEnd);
547 results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
548 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
549 results->cfaRegisterOffset);
551 case DW_CFA_def_cfa_expression:
552 results->cfaRegister = 0;
553 results->cfaExpression = (int64_t)p;
554 length = addressSpace.getULEB128(p, instructionsEnd);
555 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
556 p += static_cast<pint_t>(length);
557 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
558 ", length=%" PRIu64 ")\n",
559 results->cfaExpression, length);
561 case DW_CFA_expression:
562 reg = addressSpace.getULEB128(p, instructionsEnd);
563 if (reg > kMaxRegisterNumber) {
565 "malformed DW_CFA_expression DWARF unwind, reg too big");
568 results->savedRegisters[reg].location = kRegisterAtExpression;
569 results->savedRegisters[reg].value = (int64_t)p;
570 length = addressSpace.getULEB128(p, instructionsEnd);
571 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
572 p += static_cast<pint_t>(length);
573 _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
574 "expression=0x%" PRIx64 ", "
575 "length=%" PRIu64 ")\n",
576 reg, results->savedRegisters[reg].value, length);
578 case DW_CFA_offset_extended_sf:
579 reg = addressSpace.getULEB128(p, instructionsEnd);
580 if (reg > kMaxRegisterNumber) {
582 "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
586 addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
587 results->savedRegisters[reg].location = kRegisterInCFA;
588 results->savedRegisters[reg].value = offset;
589 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
590 "offset=%" PRId64 ")\n",
593 case DW_CFA_def_cfa_sf:
594 reg = addressSpace.getULEB128(p, instructionsEnd);
596 addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
597 if (reg > kMaxRegisterNumber) {
599 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
602 results->cfaRegister = (uint32_t)reg;
603 results->cfaRegisterOffset = (int32_t)offset;
604 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
605 "offset=%" PRId64 ")\n",
608 case DW_CFA_def_cfa_offset_sf:
609 results->cfaRegisterOffset = (int32_t)
610 (addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor);
611 results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
612 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
613 results->cfaRegisterOffset);
615 case DW_CFA_val_offset:
616 reg = addressSpace.getULEB128(p, instructionsEnd);
617 if (reg > kMaxRegisterNumber) {
619 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
624 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
625 * cieInfo.dataAlignFactor;
626 results->savedRegisters[reg].location = kRegisterOffsetFromCFA;
627 results->savedRegisters[reg].value = offset;
628 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
629 "offset=%" PRId64 "\n",
632 case DW_CFA_val_offset_sf:
633 reg = addressSpace.getULEB128(p, instructionsEnd);
634 if (reg > kMaxRegisterNumber) {
636 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
640 addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
641 results->savedRegisters[reg].location = kRegisterOffsetFromCFA;
642 results->savedRegisters[reg].value = offset;
643 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
644 "offset=%" PRId64 "\n",
647 case DW_CFA_val_expression:
648 reg = addressSpace.getULEB128(p, instructionsEnd);
649 if (reg > kMaxRegisterNumber) {
651 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
654 results->savedRegisters[reg].location = kRegisterIsExpression;
655 results->savedRegisters[reg].value = (int64_t)p;
656 length = addressSpace.getULEB128(p, instructionsEnd);
657 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
658 p += static_cast<pint_t>(length);
659 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
660 "expression=0x%" PRIx64 ", length=%" PRIu64 ")\n",
661 reg, results->savedRegisters[reg].value, length);
663 case DW_CFA_GNU_args_size:
664 length = addressSpace.getULEB128(p, instructionsEnd);
665 results->spExtraArgSize = (uint32_t)length;
666 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
668 case DW_CFA_GNU_negative_offset_extended:
669 reg = addressSpace.getULEB128(p, instructionsEnd);
670 if (reg > kMaxRegisterNumber) {
671 _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
672 "unwind, reg too big");
675 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
676 * cieInfo.dataAlignFactor;
677 results->savedRegisters[reg].location = kRegisterInCFA;
678 results->savedRegisters[reg].value = -offset;
679 _LIBUNWIND_TRACE_DWARF(
680 "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
683 #if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC)
684 // The same constant is used to represent different instructions on
685 // AArch64 (negate_ra_state) and SPARC (window_save).
686 static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
687 "uses the same constant");
688 case DW_CFA_AARCH64_negate_ra_state:
690 #if defined(_LIBUNWIND_TARGET_AARCH64)
691 case REGISTERS_ARM64:
692 results->savedRegisters[UNW_ARM64_RA_SIGN_STATE].value ^= 0x1;
693 _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
696 #if defined(_LIBUNWIND_TARGET_SPARC)
697 // case DW_CFA_GNU_window_save:
698 case REGISTERS_SPARC:
699 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
700 for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
701 results->savedRegisters[reg].location = kRegisterInRegister;
702 results->savedRegisters[reg].value =
703 ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0;
706 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
707 results->savedRegisters[reg].location = kRegisterInCFA;
708 results->savedRegisters[reg].value =
709 ((int64_t)reg - UNW_SPARC_L0) * 4;
720 operand = opcode & 0x3F;
721 switch (opcode & 0xC0) {
724 if (reg > kMaxRegisterNumber) {
725 _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
730 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
731 * cieInfo.dataAlignFactor;
732 results->savedRegisters[reg].location = kRegisterInCFA;
733 results->savedRegisters[reg].value = offset;
734 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
737 case DW_CFA_advance_loc:
738 codeOffset += operand * cieInfo.codeAlignFactor;
739 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
740 static_cast<uint64_t>(codeOffset));
744 if (reg > kMaxRegisterNumber) {
745 _LIBUNWIND_LOG("malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
750 results->savedRegisters[reg] = initialState.savedRegisters[reg];
751 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
752 static_cast<uint64_t>(operand));
755 _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
764 } // namespace libunwind
766 #endif // __DWARF_PARSER_HPP__