1 //===--------------------------- DwarfParser.hpp --------------------------===//
2 //
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
6 //
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 
20 #include "libunwind.h"
21 #include "dwarf2.h"
22 #include "Registers.hpp"
23 
24 #include "config.h"
25 
26 namespace libunwind {
27 
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
31 ///
32 template <typename A>
33 class CFI_Parser {
34 public:
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;
52 #if defined(_LIBUNWIND_TARGET_AARCH64)
53     bool      addressesSignedWithBKey;
54 #endif
55   };
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 {
68     kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
69   };
70   enum RegisterSavedWhere {
71     kRegisterUnused,
72     kRegisterInCFA,
73     kRegisterOffsetFromCFA,
74     kRegisterInRegister,
75     kRegisterAtExpression,
76     kRegisterIsExpression
77   };
78   struct RegisterLocation {
79     RegisterSavedWhere location;
80     bool initialStateSaved;
81     int64_t value;
82   };
83   /// Information about a frame layout and registers saved determined
84   /// by "running" the DWARF FDE "instructions"
85   struct PrologInfo {
86     uint32_t          cfaRegister;
87     int32_t           cfaRegisterOffset;  // CFA = (cfaRegister)+cfaRegisterOffset
88     int64_t           cfaExpression;      // CFA = expression
89     uint32_t          spExtraArgSize;
90     uint32_t          codeOffsetAtStackDecrement;
91     bool              registersInOtherRegisters;
92     bool              sameValueUsed;
93     RegisterLocation  savedRegisters[kMaxRegisterNumber + 1];
94     enum class InitializeTime { kLazy, kNormal };
95 
96     // When saving registers, this data structure is lazily initialized.
97     PrologInfo(InitializeTime IT = InitializeTime::kNormal) {
98       if (IT == InitializeTime::kNormal)
99         memset(this, 0, sizeof(*this));
100     }
101     void checkSaveRegister(uint64_t reg, PrologInfo &initialState) {
102       if (!savedRegisters[reg].initialStateSaved) {
103         initialState.savedRegisters[reg] = savedRegisters[reg];
104         savedRegisters[reg].initialStateSaved = true;
105       }
106     }
107     void setRegister(uint64_t reg, RegisterSavedWhere newLocation,
108                      int64_t newValue, PrologInfo &initialState) {
109       checkSaveRegister(reg, initialState);
110       savedRegisters[reg].location = newLocation;
111       savedRegisters[reg].value = newValue;
112     }
113     void setRegisterLocation(uint64_t reg, RegisterSavedWhere newLocation,
114                              PrologInfo &initialState) {
115       checkSaveRegister(reg, initialState);
116       savedRegisters[reg].location = newLocation;
117     }
118     void setRegisterValue(uint64_t reg, int64_t newValue,
119                           PrologInfo &initialState) {
120       checkSaveRegister(reg, initialState);
121       savedRegisters[reg].value = newValue;
122     }
123     void restoreRegisterToInitialState(uint64_t reg, PrologInfo &initialState) {
124       if (savedRegisters[reg].initialStateSaved)
125         savedRegisters[reg] = initialState.savedRegisters[reg];
126       // else the register still holds its initial state
127     }
128   };
129 
130   struct PrologInfoStackEntry {
131     PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
132         : next(n), info(i) {}
133     PrologInfoStackEntry *next;
134     PrologInfo info;
135   };
136 
137   static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
138                       uint32_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
139                       CIE_Info *cieInfo);
140   static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
141                                FDE_Info *fdeInfo, CIE_Info *cieInfo);
142   static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
143                                    const CIE_Info &cieInfo, pint_t upToPC,
144                                    int arch, PrologInfo *results);
145 
146   static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
147 
148 private:
149   static bool parseInstructions(A &addressSpace, pint_t instructions,
150                                 pint_t instructionsEnd, const CIE_Info &cieInfo,
151                                 pint_t pcoffset,
152                                 PrologInfoStackEntry *&rememberStack, int arch,
153                                 PrologInfo *results);
154 };
155 
156 /// Parse a FDE into a CIE_Info and an FDE_Info
157 template <typename A>
158 const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
159                                      FDE_Info *fdeInfo, CIE_Info *cieInfo) {
160   pint_t p = fdeStart;
161   pint_t cfiLength = (pint_t)addressSpace.get32(p);
162   p += 4;
163   if (cfiLength == 0xffffffff) {
164     // 0xffffffff means length is really next 8 bytes
165     cfiLength = (pint_t)addressSpace.get64(p);
166     p += 8;
167   }
168   if (cfiLength == 0)
169     return "FDE has zero length"; // end marker
170   uint32_t ciePointer = addressSpace.get32(p);
171   if (ciePointer == 0)
172     return "FDE is really a CIE"; // this is a CIE not an FDE
173   pint_t nextCFI = p + cfiLength;
174   pint_t cieStart = p - ciePointer;
175   const char *err = parseCIE(addressSpace, cieStart, cieInfo);
176   if (err != NULL)
177     return err;
178   p += 4;
179   // Parse pc begin and range.
180   pint_t pcStart =
181       addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
182   pint_t pcRange =
183       addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
184   // Parse rest of info.
185   fdeInfo->lsda = 0;
186   // Check for augmentation length.
187   if (cieInfo->fdesHaveAugmentationData) {
188     pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
189     pint_t endOfAug = p + augLen;
190     if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
191       // Peek at value (without indirection).  Zero means no LSDA.
192       pint_t lsdaStart = p;
193       if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
194           0) {
195         // Reset pointer and re-parse LSDA address.
196         p = lsdaStart;
197         fdeInfo->lsda =
198             addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
199       }
200     }
201     p = endOfAug;
202   }
203   fdeInfo->fdeStart = fdeStart;
204   fdeInfo->fdeLength = nextCFI - fdeStart;
205   fdeInfo->fdeInstructions = p;
206   fdeInfo->pcStart = pcStart;
207   fdeInfo->pcEnd = pcStart + pcRange;
208   return NULL; // success
209 }
210 
211 /// Scan an eh_frame section to find an FDE for a pc
212 template <typename A>
213 bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
214                             uint32_t sectionLength, pint_t fdeHint,
215                             FDE_Info *fdeInfo, CIE_Info *cieInfo) {
216   //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
217   pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
218   const pint_t ehSectionEnd = p + sectionLength;
219   while (p < ehSectionEnd) {
220     pint_t currentCFI = p;
221     //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
222     pint_t cfiLength = addressSpace.get32(p);
223     p += 4;
224     if (cfiLength == 0xffffffff) {
225       // 0xffffffff means length is really next 8 bytes
226       cfiLength = (pint_t)addressSpace.get64(p);
227       p += 8;
228     }
229     if (cfiLength == 0)
230       return false; // end marker
231     uint32_t id = addressSpace.get32(p);
232     if (id == 0) {
233       // Skip over CIEs.
234       p += cfiLength;
235     } else {
236       // Process FDE to see if it covers pc.
237       pint_t nextCFI = p + cfiLength;
238       uint32_t ciePointer = addressSpace.get32(p);
239       pint_t cieStart = p - ciePointer;
240       // Validate pointer to CIE is within section.
241       if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
242         if (parseCIE(addressSpace, cieStart, cieInfo) == NULL) {
243           p += 4;
244           // Parse pc begin and range.
245           pint_t pcStart =
246               addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
247           pint_t pcRange = addressSpace.getEncodedP(
248               p, nextCFI, cieInfo->pointerEncoding & 0x0F);
249           // Test if pc is within the function this FDE covers.
250           if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
251             // parse rest of info
252             fdeInfo->lsda = 0;
253             // check for augmentation length
254             if (cieInfo->fdesHaveAugmentationData) {
255               pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
256               pint_t endOfAug = p + augLen;
257               if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
258                 // Peek at value (without indirection).  Zero means no LSDA.
259                 pint_t lsdaStart = p;
260                 if (addressSpace.getEncodedP(
261                         p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
262                   // Reset pointer and re-parse LSDA address.
263                   p = lsdaStart;
264                   fdeInfo->lsda = addressSpace
265                       .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
266                 }
267               }
268               p = endOfAug;
269             }
270             fdeInfo->fdeStart = currentCFI;
271             fdeInfo->fdeLength = nextCFI - currentCFI;
272             fdeInfo->fdeInstructions = p;
273             fdeInfo->pcStart = pcStart;
274             fdeInfo->pcEnd = pcStart + pcRange;
275             return true;
276           } else {
277             // pc is not in begin/range, skip this FDE
278           }
279         } else {
280           // Malformed CIE, now augmentation describing pc range encoding.
281         }
282       } else {
283         // malformed FDE.  CIE is bad
284       }
285       p = nextCFI;
286     }
287   }
288   return false;
289 }
290 
291 /// Extract info from a CIE
292 template <typename A>
293 const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
294                                     CIE_Info *cieInfo) {
295   cieInfo->pointerEncoding = 0;
296   cieInfo->lsdaEncoding = DW_EH_PE_omit;
297   cieInfo->personalityEncoding = 0;
298   cieInfo->personalityOffsetInCIE = 0;
299   cieInfo->personality = 0;
300   cieInfo->codeAlignFactor = 0;
301   cieInfo->dataAlignFactor = 0;
302   cieInfo->isSignalFrame = false;
303   cieInfo->fdesHaveAugmentationData = false;
304 #if defined(_LIBUNWIND_TARGET_AARCH64)
305   cieInfo->addressesSignedWithBKey = false;
306 #endif
307   cieInfo->cieStart = cie;
308   pint_t p = cie;
309   pint_t cieLength = (pint_t)addressSpace.get32(p);
310   p += 4;
311   pint_t cieContentEnd = p + cieLength;
312   if (cieLength == 0xffffffff) {
313     // 0xffffffff means length is really next 8 bytes
314     cieLength = (pint_t)addressSpace.get64(p);
315     p += 8;
316     cieContentEnd = p + cieLength;
317   }
318   if (cieLength == 0)
319     return NULL;
320   // CIE ID is always 0
321   if (addressSpace.get32(p) != 0)
322     return "CIE ID is not zero";
323   p += 4;
324   // Version is always 1 or 3
325   uint8_t version = addressSpace.get8(p);
326   if ((version != 1) && (version != 3))
327     return "CIE version is not 1 or 3";
328   ++p;
329   // save start of augmentation string and find end
330   pint_t strStart = p;
331   while (addressSpace.get8(p) != 0)
332     ++p;
333   ++p;
334   // parse code aligment factor
335   cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
336   // parse data alignment factor
337   cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
338   // parse return address register
339   uint64_t raReg = (version == 1) ? addressSpace.get8(p++)
340                                   : addressSpace.getULEB128(p, cieContentEnd);
341   assert(raReg < 255 && "return address register too large");
342   cieInfo->returnAddressRegister = (uint8_t)raReg;
343   // parse augmentation data based on augmentation string
344   const char *result = NULL;
345   if (addressSpace.get8(strStart) == 'z') {
346     // parse augmentation data length
347     addressSpace.getULEB128(p, cieContentEnd);
348     for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
349       switch (addressSpace.get8(s)) {
350       case 'z':
351         cieInfo->fdesHaveAugmentationData = true;
352         break;
353       case 'P':
354         cieInfo->personalityEncoding = addressSpace.get8(p);
355         ++p;
356         cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
357         cieInfo->personality = addressSpace
358             .getEncodedP(p, cieContentEnd, cieInfo->personalityEncoding);
359         break;
360       case 'L':
361         cieInfo->lsdaEncoding = addressSpace.get8(p);
362         ++p;
363         break;
364       case 'R':
365         cieInfo->pointerEncoding = addressSpace.get8(p);
366         ++p;
367         break;
368       case 'S':
369         cieInfo->isSignalFrame = true;
370         break;
371 #if defined(_LIBUNWIND_TARGET_AARCH64)
372       case 'B':
373         cieInfo->addressesSignedWithBKey = true;
374         break;
375 #endif
376       default:
377         // ignore unknown letters
378         break;
379       }
380     }
381   }
382   cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
383   cieInfo->cieInstructions = p;
384   return result;
385 }
386 
387 
388 /// "run" the DWARF instructions and create the abstact PrologInfo for an FDE
389 template <typename A>
390 bool CFI_Parser<A>::parseFDEInstructions(A &addressSpace,
391                                          const FDE_Info &fdeInfo,
392                                          const CIE_Info &cieInfo, pint_t upToPC,
393                                          int arch, PrologInfo *results) {
394   PrologInfoStackEntry *rememberStack = NULL;
395 
396   // parse CIE then FDE instructions
397   bool returnValue =
398       parseInstructions(addressSpace, cieInfo.cieInstructions,
399                         cieInfo.cieStart + cieInfo.cieLength, cieInfo,
400                         (pint_t)(-1), rememberStack, arch, results) &&
401       parseInstructions(addressSpace, fdeInfo.fdeInstructions,
402                         fdeInfo.fdeStart + fdeInfo.fdeLength, cieInfo,
403                         upToPC - fdeInfo.pcStart, rememberStack, arch, results);
404 
405 #if !defined(_LIBUNWIND_NO_HEAP)
406   // Clean up rememberStack. Even in the case where every DW_CFA_remember_state
407   // is paired with a DW_CFA_restore_state, parseInstructions can skip restore
408   // opcodes if it reaches the target PC and stops interpreting, so we have to
409   // make sure we don't leak memory.
410   while (rememberStack) {
411     PrologInfoStackEntry *next = rememberStack->next;
412     free(rememberStack);
413     rememberStack = next;
414   }
415 #endif
416 
417   return returnValue;
418 }
419 
420 /// "run" the DWARF instructions
421 template <typename A>
422 bool CFI_Parser<A>::parseInstructions(A &addressSpace, pint_t instructions,
423                                       pint_t instructionsEnd,
424                                       const CIE_Info &cieInfo, pint_t pcoffset,
425                                       PrologInfoStackEntry *&rememberStack,
426                                       int arch, PrologInfo *results) {
427   pint_t p = instructions;
428   pint_t codeOffset = 0;
429   // initialState initialized as registers in results are modified. Use
430   // PrologInfo accessor functions to avoid reading uninitialized data.
431   PrologInfo initialState(PrologInfo::InitializeTime::kLazy);
432 
433   _LIBUNWIND_TRACE_DWARF("parseInstructions(instructions=0x%0" PRIx64 ")\n",
434                          static_cast<uint64_t>(instructionsEnd));
435 
436   // see DWARF Spec, section 6.4.2 for details on unwind opcodes
437   while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
438     uint64_t reg;
439     uint64_t reg2;
440     int64_t offset;
441     uint64_t length;
442     uint8_t opcode = addressSpace.get8(p);
443     uint8_t operand;
444 #if !defined(_LIBUNWIND_NO_HEAP)
445     PrologInfoStackEntry *entry;
446 #endif
447     ++p;
448     switch (opcode) {
449     case DW_CFA_nop:
450       _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
451       break;
452     case DW_CFA_set_loc:
453       codeOffset =
454           addressSpace.getEncodedP(p, instructionsEnd, cieInfo.pointerEncoding);
455       _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
456       break;
457     case DW_CFA_advance_loc1:
458       codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
459       p += 1;
460       _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
461                              static_cast<uint64_t>(codeOffset));
462       break;
463     case DW_CFA_advance_loc2:
464       codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
465       p += 2;
466       _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
467                              static_cast<uint64_t>(codeOffset));
468       break;
469     case DW_CFA_advance_loc4:
470       codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
471       p += 4;
472       _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
473                              static_cast<uint64_t>(codeOffset));
474       break;
475     case DW_CFA_offset_extended:
476       reg = addressSpace.getULEB128(p, instructionsEnd);
477       offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
478                                                   * cieInfo.dataAlignFactor;
479       if (reg > kMaxRegisterNumber) {
480         _LIBUNWIND_LOG0(
481                 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
482         return false;
483       }
484       results->setRegister(reg, kRegisterInCFA, offset, initialState);
485       _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
486                              "offset=%" PRId64 ")\n",
487                              reg, offset);
488       break;
489     case DW_CFA_restore_extended:
490       reg = addressSpace.getULEB128(p, instructionsEnd);
491       if (reg > kMaxRegisterNumber) {
492         _LIBUNWIND_LOG0(
493             "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
494         return false;
495       }
496       results->restoreRegisterToInitialState(reg, initialState);
497       _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n", reg);
498       break;
499     case DW_CFA_undefined:
500       reg = addressSpace.getULEB128(p, instructionsEnd);
501       if (reg > kMaxRegisterNumber) {
502         _LIBUNWIND_LOG0(
503                 "malformed DW_CFA_undefined DWARF unwind, reg too big");
504         return false;
505       }
506       results->setRegisterLocation(reg, kRegisterUnused, initialState);
507       _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
508       break;
509     case DW_CFA_same_value:
510       reg = addressSpace.getULEB128(p, instructionsEnd);
511       if (reg > kMaxRegisterNumber) {
512         _LIBUNWIND_LOG0(
513                 "malformed DW_CFA_same_value DWARF unwind, reg too big");
514         return false;
515       }
516       // <rdar://problem/8456377> DW_CFA_same_value unsupported
517       // "same value" means register was stored in frame, but its current
518       // value has not changed, so no need to restore from frame.
519       // We model this as if the register was never saved.
520       results->setRegisterLocation(reg, kRegisterUnused, initialState);
521       // set flag to disable conversion to compact unwind
522       results->sameValueUsed = true;
523       _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
524       break;
525     case DW_CFA_register:
526       reg = addressSpace.getULEB128(p, instructionsEnd);
527       reg2 = addressSpace.getULEB128(p, instructionsEnd);
528       if (reg > kMaxRegisterNumber) {
529         _LIBUNWIND_LOG0(
530                 "malformed DW_CFA_register DWARF unwind, reg too big");
531         return false;
532       }
533       if (reg2 > kMaxRegisterNumber) {
534         _LIBUNWIND_LOG0(
535                 "malformed DW_CFA_register DWARF unwind, reg2 too big");
536         return false;
537       }
538       results->setRegister(reg, kRegisterInRegister, (int64_t)reg2,
539                            initialState);
540       // set flag to disable conversion to compact unwind
541       results->registersInOtherRegisters = true;
542       _LIBUNWIND_TRACE_DWARF(
543           "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
544       break;
545 #if !defined(_LIBUNWIND_NO_HEAP)
546     case DW_CFA_remember_state:
547       // avoid operator new, because that would be an upward dependency
548       entry = (PrologInfoStackEntry *)malloc(sizeof(PrologInfoStackEntry));
549       if (entry != NULL) {
550         entry->next = rememberStack;
551         entry->info = *results;
552         rememberStack = entry;
553       } else {
554         return false;
555       }
556       _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
557       break;
558     case DW_CFA_restore_state:
559       if (rememberStack != NULL) {
560         PrologInfoStackEntry *top = rememberStack;
561         *results = top->info;
562         rememberStack = top->next;
563         free((char *)top);
564       } else {
565         return false;
566       }
567       _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
568       break;
569 #endif
570     case DW_CFA_def_cfa:
571       reg = addressSpace.getULEB128(p, instructionsEnd);
572       offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
573       if (reg > kMaxRegisterNumber) {
574         _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
575         return false;
576       }
577       results->cfaRegister = (uint32_t)reg;
578       results->cfaRegisterOffset = (int32_t)offset;
579       _LIBUNWIND_TRACE_DWARF(
580           "DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64 ")\n", reg, offset);
581       break;
582     case DW_CFA_def_cfa_register:
583       reg = addressSpace.getULEB128(p, instructionsEnd);
584       if (reg > kMaxRegisterNumber) {
585         _LIBUNWIND_LOG0(
586             "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
587         return false;
588       }
589       results->cfaRegister = (uint32_t)reg;
590       _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
591       break;
592     case DW_CFA_def_cfa_offset:
593       results->cfaRegisterOffset = (int32_t)
594                                   addressSpace.getULEB128(p, instructionsEnd);
595       results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
596       _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
597                              results->cfaRegisterOffset);
598       break;
599     case DW_CFA_def_cfa_expression:
600       results->cfaRegister = 0;
601       results->cfaExpression = (int64_t)p;
602       length = addressSpace.getULEB128(p, instructionsEnd);
603       assert(length < static_cast<pint_t>(~0) && "pointer overflow");
604       p += static_cast<pint_t>(length);
605       _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
606                              ", length=%" PRIu64 ")\n",
607                              results->cfaExpression, length);
608       break;
609     case DW_CFA_expression:
610       reg = addressSpace.getULEB128(p, instructionsEnd);
611       if (reg > kMaxRegisterNumber) {
612         _LIBUNWIND_LOG0(
613                 "malformed DW_CFA_expression DWARF unwind, reg too big");
614         return false;
615       }
616       results->setRegister(reg, kRegisterAtExpression, (int64_t)p,
617                            initialState);
618       length = addressSpace.getULEB128(p, instructionsEnd);
619       assert(length < static_cast<pint_t>(~0) && "pointer overflow");
620       p += static_cast<pint_t>(length);
621       _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
622                              "expression=0x%" PRIx64 ", "
623                              "length=%" PRIu64 ")\n",
624                              reg, results->savedRegisters[reg].value, length);
625       break;
626     case DW_CFA_offset_extended_sf:
627       reg = addressSpace.getULEB128(p, instructionsEnd);
628       if (reg > kMaxRegisterNumber) {
629         _LIBUNWIND_LOG0(
630             "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
631         return false;
632       }
633       offset =
634           addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
635       results->setRegister(reg, kRegisterInCFA, offset, initialState);
636       _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
637                              "offset=%" PRId64 ")\n",
638                              reg, offset);
639       break;
640     case DW_CFA_def_cfa_sf:
641       reg = addressSpace.getULEB128(p, instructionsEnd);
642       offset =
643           addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
644       if (reg > kMaxRegisterNumber) {
645         _LIBUNWIND_LOG0(
646                 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
647         return false;
648       }
649       results->cfaRegister = (uint32_t)reg;
650       results->cfaRegisterOffset = (int32_t)offset;
651       _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
652                              "offset=%" PRId64 ")\n",
653                              reg, offset);
654       break;
655     case DW_CFA_def_cfa_offset_sf:
656       results->cfaRegisterOffset = (int32_t)
657         (addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor);
658       results->codeOffsetAtStackDecrement = (uint32_t)codeOffset;
659       _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
660                              results->cfaRegisterOffset);
661       break;
662     case DW_CFA_val_offset:
663       reg = addressSpace.getULEB128(p, instructionsEnd);
664       if (reg > kMaxRegisterNumber) {
665         _LIBUNWIND_LOG(
666                 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
667                 ") out of range\n",
668                 reg);
669         return false;
670       }
671       offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
672                                                     * cieInfo.dataAlignFactor;
673       results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
674       _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
675                              "offset=%" PRId64 "\n",
676                              reg, offset);
677       break;
678     case DW_CFA_val_offset_sf:
679       reg = addressSpace.getULEB128(p, instructionsEnd);
680       if (reg > kMaxRegisterNumber) {
681         _LIBUNWIND_LOG0(
682                 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
683         return false;
684       }
685       offset =
686           addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
687       results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
688       _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
689                              "offset=%" PRId64 "\n",
690                              reg, offset);
691       break;
692     case DW_CFA_val_expression:
693       reg = addressSpace.getULEB128(p, instructionsEnd);
694       if (reg > kMaxRegisterNumber) {
695         _LIBUNWIND_LOG0(
696                 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
697         return false;
698       }
699       results->setRegister(reg, kRegisterIsExpression, (int64_t)p,
700                            initialState);
701       length = addressSpace.getULEB128(p, instructionsEnd);
702       assert(length < static_cast<pint_t>(~0) && "pointer overflow");
703       p += static_cast<pint_t>(length);
704       _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
705                              "expression=0x%" PRIx64 ", length=%" PRIu64 ")\n",
706                              reg, results->savedRegisters[reg].value, length);
707       break;
708     case DW_CFA_GNU_args_size:
709       length = addressSpace.getULEB128(p, instructionsEnd);
710       results->spExtraArgSize = (uint32_t)length;
711       _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
712       break;
713     case DW_CFA_GNU_negative_offset_extended:
714       reg = addressSpace.getULEB128(p, instructionsEnd);
715       if (reg > kMaxRegisterNumber) {
716         _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
717                         "unwind, reg too big");
718         return false;
719       }
720       offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd)
721                                                     * cieInfo.dataAlignFactor;
722       results->setRegister(reg, kRegisterInCFA, -offset, initialState);
723       _LIBUNWIND_TRACE_DWARF(
724           "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
725       break;
726 
727 #if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC)
728     // The same constant is used to represent different instructions on
729     // AArch64 (negate_ra_state) and SPARC (window_save).
730     static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
731                   "uses the same constant");
732     case DW_CFA_AARCH64_negate_ra_state:
733       switch (arch) {
734 #if defined(_LIBUNWIND_TARGET_AARCH64)
735         case REGISTERS_ARM64: {
736           int64_t value =
737               results->savedRegisters[UNW_ARM64_RA_SIGN_STATE].value ^ 0x1;
738           results->setRegisterValue(UNW_ARM64_RA_SIGN_STATE, value, initialState);
739           _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
740         } break;
741 #endif
742 
743 #if defined(_LIBUNWIND_TARGET_SPARC)
744       // case DW_CFA_GNU_window_save:
745       case REGISTERS_SPARC:
746         _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
747         for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
748           results->setRegister(reg, kRegisterInRegister,
749                                ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0,
750                                initialState);
751         }
752 
753         for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
754           results->setRegister(reg, kRegisterInCFA,
755                                ((int64_t)reg - UNW_SPARC_L0) * 4, initialState);
756         }
757         break;
758 #endif
759       }
760       break;
761 #else
762       (void)arch;
763 #endif
764 
765     default:
766       operand = opcode & 0x3F;
767       switch (opcode & 0xC0) {
768       case DW_CFA_offset:
769         reg = operand;
770         if (reg > kMaxRegisterNumber) {
771           _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
772                          ") out of range",
773                   reg);
774           return false;
775         }
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",
780                                operand, offset);
781         break;
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));
786         break;
787       case DW_CFA_restore:
788         reg = operand;
789         if (reg > kMaxRegisterNumber) {
790           _LIBUNWIND_LOG("malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
791                          ") out of range",
792                   reg);
793           return false;
794         }
795         results->restoreRegisterToInitialState(reg, initialState);
796         _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
797                                static_cast<uint64_t>(operand));
798         break;
799       default:
800         _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
801         return false;
802       }
803     }
804   }
805 
806   return true;
807 }
808 
809 } // namespace libunwind
810 
811 #endif // __DWARF_PARSER_HPP__
812