1//===----------------------------------------------------------------------===//
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#if defined(_LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING)
27#include <ptrauth.h>
28#endif
29
30namespace libunwind {
31
32/// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
33/// See DWARF Spec for details:
34/// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
35///
36template <typename A>
37class CFI_Parser {
38public:
39 typedef typename A::pint_t pint_t;
40 typedef pint_t __ptrauth_unwind_cie_info_personality personality_t;
41
42 /// Information encoded in a CIE (Common Information Entry)
43 struct CIE_Info {
44 pint_t cieStart;
45 pint_t cieLength;
46 pint_t cieInstructions;
47 uint8_t pointerEncoding;
48 uint8_t lsdaEncoding;
49 uint8_t personalityEncoding;
50 uint8_t personalityOffsetInCIE;
51 personality_t personality;
52 uint32_t codeAlignFactor;
53 int dataAlignFactor;
54 bool isSignalFrame;
55 bool fdesHaveAugmentationData;
56 uint8_t returnAddressRegister;
57#if defined(_LIBUNWIND_TARGET_AARCH64)
58 bool addressesSignedWithBKey;
59 bool mteTaggedFrame;
60 bool nonHintPAuthFrame;
61#endif
62 };
63
64 /// Information about an FDE (Frame Description Entry)
65 struct FDE_Info {
66 pint_t fdeStart;
67 pint_t fdeLength;
68 pint_t fdeInstructions;
69 pint_t pcStart;
70 pint_t pcEnd;
71 pint_t lsda;
72 };
73
74 enum {
75 kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
76 };
77 enum RegisterSavedWhere {
78 kRegisterUnused,
79 kRegisterUndefined,
80 kRegisterInCFA,
81 kRegisterInCFADecrypt, // sparc64 specific
82 kRegisterOffsetFromCFA,
83 kRegisterInRegister,
84 kRegisterAtExpression,
85 kRegisterIsExpression,
86 kRegisterIsPseudo,
87 };
88 struct RegisterLocation {
89 RegisterSavedWhere location;
90 bool initialStateSaved;
91 int64_t value;
92 };
93 /// Information about a frame layout and registers saved determined
94 /// by "running" the DWARF FDE "instructions"
95 struct PrologInfo {
96 uint32_t cfaRegister;
97 int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset
98 int64_t cfaExpression; // CFA = expression
99 uint32_t spExtraArgSize;
100 RegisterLocation savedRegisters[kMaxRegisterNumber + 1];
101#if defined(_LIBUNWIND_TARGET_AARCH64)
102 pint_t ptrAuthDiversifier;
103#endif
104 enum class InitializeTime { kLazy, kNormal };
105
106 // When saving registers, this data structure is lazily initialized.
107 PrologInfo(InitializeTime IT = InitializeTime::kNormal) {
108 if (IT == InitializeTime::kNormal)
109 memset(this, 0, sizeof(*this));
110 }
111 void checkSaveRegister(uint64_t reg, PrologInfo &initialState) {
112 if (!savedRegisters[reg].initialStateSaved) {
113 initialState.savedRegisters[reg] = savedRegisters[reg];
114 savedRegisters[reg].initialStateSaved = true;
115 }
116 }
117 void setRegister(uint64_t reg, RegisterSavedWhere newLocation,
118 int64_t newValue, PrologInfo &initialState) {
119 checkSaveRegister(reg, initialState);
120 savedRegisters[reg].location = newLocation;
121 savedRegisters[reg].value = newValue;
122 }
123 void setRegisterLocation(uint64_t reg, RegisterSavedWhere newLocation,
124 PrologInfo &initialState) {
125 checkSaveRegister(reg, initialState);
126 savedRegisters[reg].location = newLocation;
127 }
128 void setRegisterValue(uint64_t reg, int64_t newValue,
129 PrologInfo &initialState) {
130 checkSaveRegister(reg, initialState);
131 savedRegisters[reg].value = newValue;
132 }
133 void restoreRegisterToInitialState(uint64_t reg, PrologInfo &initialState) {
134 if (savedRegisters[reg].initialStateSaved)
135 savedRegisters[reg] = initialState.savedRegisters[reg];
136 // else the register still holds its initial state
137 }
138 };
139
140 struct PrologInfoStackEntry {
141 PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
142 : next(n), info(i) {}
143 PrologInfoStackEntry *next;
144 PrologInfo info;
145 };
146
147 struct RememberStack {
148 PrologInfoStackEntry *entry;
149 RememberStack() : entry(nullptr) {}
150 ~RememberStack() {
151#if defined(_LIBUNWIND_REMEMBER_CLEANUP_NEEDED)
152 // Clean up rememberStack. Even in the case where every
153 // DW_CFA_remember_state is paired with a DW_CFA_restore_state,
154 // parseInstructions can skip restore opcodes if it reaches the target PC
155 // and stops interpreting, so we have to make sure we don't leak memory.
156 while (entry) {
157 PrologInfoStackEntry *next = entry->next;
158 _LIBUNWIND_REMEMBER_FREE(entry);
159 entry = next;
160 }
161#endif
162 }
163 };
164
165 template <typename R>
166 static bool findFDE(A &addressSpace, typename R::link_hardened_reg_arg_t pc,
167 pint_t ehSectionStart, size_t sectionLength,
168 pint_t fdeHint, FDE_Info *fdeInfo, CIE_Info *cieInfo);
169 static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
170 FDE_Info *fdeInfo, CIE_Info *cieInfo,
171 bool useCIEInfo = false);
172 template <typename R>
173 static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
174 const CIE_Info &cieInfo,
175 typename R::link_hardened_reg_arg_t upToPC,
176 int arch, PrologInfo *results);
177
178 static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
179};
180
181/// Parse a FDE into a CIE_Info and an FDE_Info. If useCIEInfo is
182/// true, treat cieInfo as already-parsed CIE_Info (whose start offset
183/// must match the one specified by the FDE) rather than parsing the
184/// one indicated within the FDE.
185template <typename A>
186const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
187 FDE_Info *fdeInfo, CIE_Info *cieInfo,
188 bool useCIEInfo) {
189 pint_t p = fdeStart;
190 pint_t cfiLength = (pint_t)addressSpace.get32(p);
191 p += 4;
192 if (cfiLength == 0xffffffff) {
193 // 0xffffffff means length is really next 8 bytes
194 cfiLength = (pint_t)addressSpace.get64(p);
195 p += 8;
196 }
197 if (cfiLength == 0)
198 return "FDE has zero length"; // zero terminator
199 uint32_t ciePointer = addressSpace.get32(p);
200 if (ciePointer == 0)
201 return "FDE is really a CIE"; // this is a CIE not an FDE
202 pint_t nextCFI = p + cfiLength;
203 pint_t cieStart = p - ciePointer;
204 if (useCIEInfo) {
205 if (cieInfo->cieStart != cieStart)
206 return "CIE start does not match";
207 } else {
208 const char *err = parseCIE(addressSpace, cie: cieStart, cieInfo);
209 if (err != NULL)
210 return err;
211 }
212 p += 4;
213 // Parse pc begin and range.
214 pint_t pcStart =
215 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
216 pint_t pcRange =
217 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
218 // Parse rest of info.
219 fdeInfo->lsda = 0;
220 // Check for augmentation length.
221 if (cieInfo->fdesHaveAugmentationData) {
222 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
223 pint_t endOfAug = p + augLen;
224 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
225 // Peek at value (without indirection). Zero means no LSDA.
226 pint_t lsdaStart = p;
227 if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
228 0) {
229 // Reset pointer and re-parse LSDA address.
230 p = lsdaStart;
231 fdeInfo->lsda =
232 addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
233 }
234 }
235 p = endOfAug;
236 }
237 fdeInfo->fdeStart = fdeStart;
238 fdeInfo->fdeLength = nextCFI - fdeStart;
239 fdeInfo->fdeInstructions = p;
240 fdeInfo->pcStart = pcStart;
241 fdeInfo->pcEnd = pcStart + pcRange;
242 return NULL; // success
243}
244
245/// Scan an eh_frame section to find an FDE for a pc
246template <typename A>
247template <typename R>
248bool CFI_Parser<A>::findFDE(A &addressSpace,
249 typename R::link_hardened_reg_arg_t pc,
250 pint_t ehSectionStart, size_t sectionLength,
251 pint_t fdeHint, FDE_Info *fdeInfo,
252 CIE_Info *cieInfo) {
253 //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
254 pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
255 const pint_t ehSectionEnd = (sectionLength == SIZE_MAX)
256 ? static_cast<pint_t>(-1)
257 : (ehSectionStart + sectionLength);
258 while (p < ehSectionEnd) {
259 pint_t currentCFI = p;
260 //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
261 pint_t cfiLength = addressSpace.get32(p);
262 p += 4;
263 if (cfiLength == 0xffffffff) {
264 // 0xffffffff means length is really next 8 bytes
265 cfiLength = (pint_t)addressSpace.get64(p);
266 p += 8;
267 }
268 if (cfiLength == 0)
269 return false; // zero terminator
270 uint32_t id = addressSpace.get32(p);
271 if (id == 0) {
272 // Skip over CIEs.
273 p += cfiLength;
274 } else {
275 // Process FDE to see if it covers pc.
276 pint_t nextCFI = p + cfiLength;
277 uint32_t ciePointer = addressSpace.get32(p);
278 pint_t cieStart = p - ciePointer;
279 // Validate pointer to CIE is within section.
280 if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
281 if (parseCIE(addressSpace, cie: cieStart, cieInfo) == NULL) {
282 p += 4;
283 // Parse pc begin and range.
284 pint_t pcStart =
285 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
286 pint_t pcRange = addressSpace.getEncodedP(
287 p, nextCFI, cieInfo->pointerEncoding & 0x0F);
288 // Test if pc is within the function this FDE covers.
289 if ((pcStart <= pc) && (pc < pcStart + pcRange)) {
290 // parse rest of info
291 fdeInfo->lsda = 0;
292 // check for augmentation length
293 if (cieInfo->fdesHaveAugmentationData) {
294 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
295 pint_t endOfAug = p + augLen;
296 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
297 // Peek at value (without indirection). Zero means no LSDA.
298 pint_t lsdaStart = p;
299 if (addressSpace.getEncodedP(
300 p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
301 // Reset pointer and re-parse LSDA address.
302 p = lsdaStart;
303 fdeInfo->lsda = addressSpace
304 .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
305 }
306 }
307 p = endOfAug;
308 }
309 fdeInfo->fdeStart = currentCFI;
310 fdeInfo->fdeLength = nextCFI - currentCFI;
311 fdeInfo->fdeInstructions = p;
312 fdeInfo->pcStart = pcStart;
313 fdeInfo->pcEnd = pcStart + pcRange;
314 return true;
315 } else {
316 // pc is not in begin/range, skip this FDE
317 }
318 } else {
319 // Malformed CIE, now augmentation describing pc range encoding.
320 }
321 } else {
322 // malformed FDE. CIE is bad
323 }
324 p = nextCFI;
325 }
326 }
327 return false;
328}
329
330/// Extract info from a CIE
331template <typename A>
332const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
333 CIE_Info *cieInfo) {
334 cieInfo->pointerEncoding = 0;
335 cieInfo->lsdaEncoding = DW_EH_PE_omit;
336 cieInfo->personalityEncoding = 0;
337 cieInfo->personalityOffsetInCIE = 0;
338 cieInfo->personality = 0;
339 cieInfo->codeAlignFactor = 0;
340 cieInfo->dataAlignFactor = 0;
341 cieInfo->isSignalFrame = false;
342 cieInfo->fdesHaveAugmentationData = false;
343#if defined(_LIBUNWIND_TARGET_AARCH64)
344 cieInfo->addressesSignedWithBKey = false;
345 cieInfo->mteTaggedFrame = false;
346 cieInfo->nonHintPAuthFrame = false;
347#endif
348 cieInfo->cieStart = cie;
349 pint_t p = cie;
350 pint_t cieLength = (pint_t)addressSpace.get32(p);
351 p += 4;
352 pint_t cieContentEnd = p + cieLength;
353 if (cieLength == 0xffffffff) {
354 // 0xffffffff means length is really next 8 bytes
355 cieLength = (pint_t)addressSpace.get64(p);
356 p += 8;
357 cieContentEnd = p + cieLength;
358 }
359 if (cieLength == 0)
360 return NULL;
361 // CIE ID is always 0
362 if (addressSpace.get32(p) != 0)
363 return "CIE ID is not zero";
364 p += 4;
365 // Version is always 1 or 3
366 uint8_t version = addressSpace.get8(p);
367 if ((version != 1) && (version != 3))
368 return "CIE version is not 1 or 3";
369 ++p;
370 // save start of augmentation string and find end
371 pint_t strStart = p;
372 while (addressSpace.get8(p) != 0)
373 ++p;
374 ++p;
375 // parse code alignment factor
376 cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
377 // parse data alignment factor
378 cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
379 // parse return address register
380 uint64_t raReg = (version == 1) ? addressSpace.get8(p++)
381 : addressSpace.getULEB128(p, cieContentEnd);
382 assert(raReg < 255 && "return address register too large");
383 cieInfo->returnAddressRegister = (uint8_t)raReg;
384 // parse augmentation data based on augmentation string
385 const char *result = NULL;
386 pint_t resultAddr = 0;
387 if (addressSpace.get8(strStart) == 'z') {
388 // parse augmentation data length
389 addressSpace.getULEB128(p, cieContentEnd);
390 for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
391 switch (addressSpace.get8(s)) {
392 case 'z':
393 cieInfo->fdesHaveAugmentationData = true;
394 break;
395 case 'P': {
396 cieInfo->personalityEncoding = addressSpace.get8(p);
397 ++p;
398 cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
399 pint_t personality = addressSpace.getEncodedP(
400 p, cieContentEnd, cieInfo->personalityEncoding,
401 /*datarelBase=*/0, &resultAddr);
402#if defined(_LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING)
403 if (personality) {
404 // The GOT for the personality function was signed address
405 // authenticated. Manually re-sign with the CIE_Info::personality
406 // schema. If we could guarantee the encoding of the personality we
407 // could avoid this by simply giving resultAddr the correct ptrauth
408 // schema and performing an assignment.
409#if defined(__arm64e__)
410 const auto oldDiscriminator = resultAddr;
411#else
412 const auto oldDiscriminator = ptrauth_blend_discriminator(
413 (void *)resultAddr, __ptrauth_unwind_pauthtest_personality_disc);
414#endif
415 const auto discriminator = ptrauth_blend_discriminator(
416 &cieInfo->personality,
417 __ptrauth_unwind_cie_info_personality_disc);
418 void *signedPtr = ptrauth_auth_and_resign(
419 (void *)personality, ptrauth_key_function_pointer,
420 oldDiscriminator, ptrauth_key_function_pointer, discriminator);
421 personality = (pint_t)signedPtr;
422 }
423#endif
424 // We use memmove to set the CIE personality as we have already
425 // re-signed the pointer to the correct schema.
426 memmove(dest: (void *)&cieInfo->personality, src: (void *)&personality,
427 n: sizeof(personality));
428 break;
429 }
430 case 'L':
431 cieInfo->lsdaEncoding = addressSpace.get8(p);
432 ++p;
433 break;
434 case 'R':
435 cieInfo->pointerEncoding = addressSpace.get8(p);
436 ++p;
437 break;
438 case 'S':
439 cieInfo->isSignalFrame = true;
440 break;
441#if defined(_LIBUNWIND_TARGET_AARCH64)
442 case 'B':
443 cieInfo->addressesSignedWithBKey = true;
444 break;
445 case 'G':
446 cieInfo->mteTaggedFrame = true;
447 break;
448 case 'N':
449 cieInfo->nonHintPAuthFrame = true;
450 break;
451#endif
452 default:
453 // ignore unknown letters
454 break;
455 }
456 }
457 }
458 cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
459 cieInfo->cieInstructions = p;
460 return result;
461}
462
463
464/// "run" the DWARF instructions and create the abstract PrologInfo for an FDE
465template <typename A>
466template <typename R>
467bool CFI_Parser<A>::parseFDEInstructions(
468 A &addressSpace, const FDE_Info &fdeInfo, const CIE_Info &cieInfo,
469 typename R::link_hardened_reg_arg_t upToPC, int arch, PrologInfo *results) {
470 // Alloca is used for the allocation of the rememberStack entries. It removes
471 // the dependency on new/malloc but the below for loop can not be refactored
472 // into functions. Entry could be saved during the processing of a CIE and
473 // restored by an FDE.
474 RememberStack rememberStack;
475
476 struct ParseInfo {
477 pint_t instructions;
478 pint_t instructionsEnd;
479 pint_t pcoffset;
480 };
481
482 ParseInfo parseInfoArray[] = {
483 {cieInfo.cieInstructions, cieInfo.cieStart + cieInfo.cieLength,
484 (pint_t)(-1)},
485 {fdeInfo.fdeInstructions, fdeInfo.fdeStart + fdeInfo.fdeLength,
486 static_cast<pint_t>(upToPC) - fdeInfo.pcStart}};
487
488 for (const auto &info : parseInfoArray) {
489 pint_t p = info.instructions;
490 pint_t instructionsEnd = info.instructionsEnd;
491 pint_t pcoffset = info.pcoffset;
492 pint_t codeOffset = 0;
493
494 // initialState initialized as registers in results are modified. Use
495 // PrologInfo accessor functions to avoid reading uninitialized data.
496 PrologInfo initialState(PrologInfo::InitializeTime::kLazy);
497
498 _LIBUNWIND_TRACE_DWARF("parseFDEInstructions(instructions=0x%0" PRIx64
499 ")\n",
500 static_cast<uint64_t>(instructionsEnd));
501
502 // see DWARF Spec, section 6.4.2 for details on unwind opcodes
503 while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
504 uint64_t reg;
505 uint64_t reg2;
506 int64_t offset;
507 uint64_t length;
508 uint8_t opcode = addressSpace.get8(p);
509 uint8_t operand;
510
511 ++p;
512 switch (opcode) {
513 case DW_CFA_nop:
514 _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
515 break;
516 case DW_CFA_set_loc:
517 codeOffset = addressSpace.getEncodedP(p, instructionsEnd,
518 cieInfo.pointerEncoding);
519 _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
520 break;
521 case DW_CFA_advance_loc1:
522 codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
523 p += 1;
524 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
525 static_cast<uint64_t>(codeOffset));
526 break;
527 case DW_CFA_advance_loc2:
528 codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
529 p += 2;
530 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
531 static_cast<uint64_t>(codeOffset));
532 break;
533 case DW_CFA_advance_loc4:
534 codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
535 p += 4;
536 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
537 static_cast<uint64_t>(codeOffset));
538 break;
539 case DW_CFA_offset_extended:
540 reg = addressSpace.getULEB128(p, instructionsEnd);
541 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
542 cieInfo.dataAlignFactor;
543 if (reg > kMaxRegisterNumber) {
544 _LIBUNWIND_LOG0(
545 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
546 return false;
547 }
548 results->setRegister(reg, kRegisterInCFA, offset, initialState);
549 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
550 "offset=%" PRId64 ")\n",
551 reg, offset);
552 break;
553 case DW_CFA_restore_extended:
554 reg = addressSpace.getULEB128(p, instructionsEnd);
555 if (reg > kMaxRegisterNumber) {
556 _LIBUNWIND_LOG0(
557 "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
558 return false;
559 }
560 results->restoreRegisterToInitialState(reg, initialState);
561 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n",
562 reg);
563 break;
564 case DW_CFA_undefined:
565 reg = addressSpace.getULEB128(p, instructionsEnd);
566 if (reg > kMaxRegisterNumber) {
567 _LIBUNWIND_LOG0(
568 "malformed DW_CFA_undefined DWARF unwind, reg too big");
569 return false;
570 }
571 results->setRegisterLocation(reg, kRegisterUndefined, initialState);
572 _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
573 break;
574 case DW_CFA_same_value:
575 reg = addressSpace.getULEB128(p, instructionsEnd);
576 if (reg > kMaxRegisterNumber) {
577 _LIBUNWIND_LOG0(
578 "malformed DW_CFA_same_value DWARF unwind, reg too big");
579 return false;
580 }
581 // <rdar://problem/8456377> DW_CFA_same_value unsupported
582 // "same value" means register was stored in frame, but its current
583 // value has not changed, so no need to restore from frame.
584 // We model this as if the register was never saved.
585 results->setRegisterLocation(reg, kRegisterUnused, initialState);
586 _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
587 break;
588 case DW_CFA_register:
589 reg = addressSpace.getULEB128(p, instructionsEnd);
590 reg2 = addressSpace.getULEB128(p, instructionsEnd);
591 if (reg > kMaxRegisterNumber) {
592 _LIBUNWIND_LOG0(
593 "malformed DW_CFA_register DWARF unwind, reg too big");
594 return false;
595 }
596 if (reg2 > kMaxRegisterNumber) {
597 _LIBUNWIND_LOG0(
598 "malformed DW_CFA_register DWARF unwind, reg2 too big");
599 return false;
600 }
601 results->setRegister(reg, kRegisterInRegister, (int64_t)reg2,
602 initialState);
603 _LIBUNWIND_TRACE_DWARF(
604 "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
605 break;
606 case DW_CFA_remember_state: {
607 // Avoid operator new because that would be an upward dependency.
608 // Avoid malloc because it needs heap allocation.
609 PrologInfoStackEntry *entry =
610 (PrologInfoStackEntry *)_LIBUNWIND_REMEMBER_ALLOC(
611 sizeof(PrologInfoStackEntry));
612 if (entry != NULL) {
613 entry->next = rememberStack.entry;
614 entry->info = *results;
615 rememberStack.entry = entry;
616 } else {
617 return false;
618 }
619 _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
620 break;
621 }
622 case DW_CFA_restore_state:
623 if (rememberStack.entry != NULL) {
624 PrologInfoStackEntry *top = rememberStack.entry;
625 *results = top->info;
626 rememberStack.entry = top->next;
627 _LIBUNWIND_REMEMBER_FREE(top);
628 } else {
629 return false;
630 }
631 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
632 break;
633 case DW_CFA_def_cfa:
634 reg = addressSpace.getULEB128(p, instructionsEnd);
635 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
636 if (reg > kMaxRegisterNumber) {
637 _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
638 return false;
639 }
640 results->cfaRegister = (uint32_t)reg;
641 results->cfaRegisterOffset = (int32_t)offset;
642 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64
643 ")\n",
644 reg, offset);
645 break;
646 case DW_CFA_def_cfa_register:
647 reg = addressSpace.getULEB128(p, instructionsEnd);
648 if (reg > kMaxRegisterNumber) {
649 _LIBUNWIND_LOG0(
650 "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
651 return false;
652 }
653 results->cfaRegister = (uint32_t)reg;
654 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
655 break;
656 case DW_CFA_def_cfa_offset:
657 results->cfaRegisterOffset =
658 (int32_t)addressSpace.getULEB128(p, instructionsEnd);
659 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
660 results->cfaRegisterOffset);
661 break;
662 case DW_CFA_def_cfa_expression:
663 results->cfaRegister = 0;
664 results->cfaExpression = (int64_t)p;
665 length = addressSpace.getULEB128(p, instructionsEnd);
666 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
667 p += static_cast<pint_t>(length);
668 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
669 ", length=%" PRIu64 ")\n",
670 results->cfaExpression, length);
671 break;
672 case DW_CFA_expression:
673 reg = addressSpace.getULEB128(p, instructionsEnd);
674 if (reg > kMaxRegisterNumber) {
675 _LIBUNWIND_LOG0(
676 "malformed DW_CFA_expression DWARF unwind, reg too big");
677 return false;
678 }
679 results->setRegister(reg, kRegisterAtExpression, (int64_t)p,
680 initialState);
681 length = addressSpace.getULEB128(p, instructionsEnd);
682 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
683 p += static_cast<pint_t>(length);
684 _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
685 "expression=0x%" PRIx64 ", "
686 "length=%" PRIu64 ")\n",
687 reg, results->savedRegisters[reg].value, length);
688 break;
689 case DW_CFA_offset_extended_sf:
690 reg = addressSpace.getULEB128(p, instructionsEnd);
691 if (reg > kMaxRegisterNumber) {
692 _LIBUNWIND_LOG0(
693 "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
694 return false;
695 }
696 offset = addressSpace.getSLEB128(p, instructionsEnd) *
697 cieInfo.dataAlignFactor;
698 results->setRegister(reg, kRegisterInCFA, offset, initialState);
699 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
700 "offset=%" PRId64 ")\n",
701 reg, offset);
702 break;
703 case DW_CFA_def_cfa_sf:
704 reg = addressSpace.getULEB128(p, instructionsEnd);
705 offset = addressSpace.getSLEB128(p, instructionsEnd) *
706 cieInfo.dataAlignFactor;
707 if (reg > kMaxRegisterNumber) {
708 _LIBUNWIND_LOG0(
709 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
710 return false;
711 }
712 results->cfaRegister = (uint32_t)reg;
713 results->cfaRegisterOffset = (int32_t)offset;
714 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
715 "offset=%" PRId64 ")\n",
716 reg, offset);
717 break;
718 case DW_CFA_def_cfa_offset_sf:
719 results->cfaRegisterOffset =
720 (int32_t)(addressSpace.getSLEB128(p, instructionsEnd) *
721 cieInfo.dataAlignFactor);
722 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
723 results->cfaRegisterOffset);
724 break;
725 case DW_CFA_val_offset:
726 reg = addressSpace.getULEB128(p, instructionsEnd);
727 if (reg > kMaxRegisterNumber) {
728 _LIBUNWIND_LOG(
729 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
730 ") out of range\n",
731 reg);
732 return false;
733 }
734 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
735 cieInfo.dataAlignFactor;
736 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
737 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
738 "offset=%" PRId64 "\n",
739 reg, offset);
740 break;
741 case DW_CFA_val_offset_sf:
742 reg = addressSpace.getULEB128(p, instructionsEnd);
743 if (reg > kMaxRegisterNumber) {
744 _LIBUNWIND_LOG0(
745 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
746 return false;
747 }
748 offset = addressSpace.getSLEB128(p, instructionsEnd) *
749 cieInfo.dataAlignFactor;
750 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
751 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
752 "offset=%" PRId64 "\n",
753 reg, offset);
754 break;
755 case DW_CFA_val_expression:
756 reg = addressSpace.getULEB128(p, instructionsEnd);
757 if (reg > kMaxRegisterNumber) {
758 _LIBUNWIND_LOG0(
759 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
760 return false;
761 }
762 results->setRegister(reg, kRegisterIsExpression, (int64_t)p,
763 initialState);
764 length = addressSpace.getULEB128(p, instructionsEnd);
765 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
766 p += static_cast<pint_t>(length);
767 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
768 "expression=0x%" PRIx64 ", length=%" PRIu64
769 ")\n",
770 reg, results->savedRegisters[reg].value, length);
771 break;
772 case DW_CFA_GNU_args_size:
773 length = addressSpace.getULEB128(p, instructionsEnd);
774 results->spExtraArgSize = (uint32_t)length;
775 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
776 break;
777 case DW_CFA_GNU_negative_offset_extended:
778 reg = addressSpace.getULEB128(p, instructionsEnd);
779 if (reg > kMaxRegisterNumber) {
780 _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
781 "unwind, reg too big");
782 return false;
783 }
784 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
785 cieInfo.dataAlignFactor;
786 results->setRegister(reg, kRegisterInCFA, -offset, initialState);
787 _LIBUNWIND_TRACE_DWARF(
788 "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
789 break;
790
791#if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC) || \
792 defined(_LIBUNWIND_TARGET_SPARC64)
793 // The same constant is used to represent different instructions on
794 // AArch64 (negate_ra_state) and SPARC (window_save).
795 static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
796 "uses the same constant");
797 case DW_CFA_AARCH64_negate_ra_state:
798 switch (arch) {
799#if defined(_LIBUNWIND_TARGET_AARCH64)
800 case REGISTERS_ARM64: {
801 int64_t value =
802 results->savedRegisters[UNW_AARCH64_RA_SIGN_STATE].value ^ 0x1;
803 results->setRegister(UNW_AARCH64_RA_SIGN_STATE, kRegisterIsPseudo,
804 value, initialState);
805 _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
806 } break;
807#endif
808
809#if defined(_LIBUNWIND_TARGET_SPARC)
810 // case DW_CFA_GNU_window_save:
811 case REGISTERS_SPARC:
812 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
813 for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
814 results->setRegister(reg, kRegisterInRegister,
815 ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0,
816 initialState);
817 }
818
819 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
820 results->setRegister(reg, kRegisterInCFA,
821 ((int64_t)reg - UNW_SPARC_L0) * 4,
822 initialState);
823 }
824 break;
825#endif
826
827#if defined(_LIBUNWIND_TARGET_SPARC64)
828 // case DW_CFA_GNU_window_save:
829 case REGISTERS_SPARC64:
830 // Don't save %o0-%o7 on sparc64.
831 // https://reviews.llvm.org/D32450#736405
832
833 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
834 if (reg == UNW_SPARC_I7)
835 results->setRegister(
836 reg, kRegisterInCFADecrypt,
837 static_cast<int64_t>((reg - UNW_SPARC_L0) * sizeof(pint_t)),
838 initialState);
839 else
840 results->setRegister(
841 reg, kRegisterInCFA,
842 static_cast<int64_t>((reg - UNW_SPARC_L0) * sizeof(pint_t)),
843 initialState);
844 }
845 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save\n");
846 break;
847#endif
848 }
849 break;
850
851#if defined(_LIBUNWIND_TARGET_AARCH64)
852 case DW_CFA_AARCH64_negate_ra_state_with_pc: {
853 int64_t value =
854 results->savedRegisters[UNW_AARCH64_RA_SIGN_STATE].value ^ 0x3;
855 results->setRegister(UNW_AARCH64_RA_SIGN_STATE, kRegisterIsPseudo,
856 value, initialState);
857 // When using Feat_PAuthLR, the PC value needs to be captured so that
858 // during unwinding, the correct PC value is used for re-authentication.
859 // It is assumed that the CFI is placed before the signing instruction.
860 results->ptrAuthDiversifier = fdeInfo.pcStart + codeOffset;
861 _LIBUNWIND_TRACE_DWARF(
862 "DW_CFA_AARCH64_negate_ra_state_with_pc(pc=0x%" PRIx64 ")\n",
863 static_cast<uint64_t>(results->ptrAuthDiversifier));
864 } break;
865#endif
866
867#if defined(_LIBUNWIND_TARGET_AARCH64)
868 case DW_CFA_AARCH64_set_ra_state: {
869 int64_t value =
870 static_cast<int64_t>(addressSpace.getULEB128(p, instructionsEnd));
871 if (value < 0 || 2 < value) {
872 _LIBUNWIND_LOG0("malformed DW_CFA_AARCH64_set_ra_state DWARF "
873 "unwind, RA_SIGN_STATE value not recognized");
874 return false;
875 }
876 offset = addressSpace.getSLEB128(p, instructionsEnd) *
877 cieInfo.codeAlignFactor;
878 results->setRegister(UNW_AARCH64_RA_SIGN_STATE, kRegisterIsPseudo,
879 value, initialState);
880 results->ptrAuthDiversifier = fdeInfo.pcStart + codeOffset + offset;
881 _LIBUNWIND_TRACE_DWARF(
882 "DW_CFA_AARCH64_set_ra_state(state=%" PRId64 ",pc=0x%" PRIx64 ")\n",
883 value, static_cast<uint64_t>(results->ptrAuthDiversifier));
884 } break;
885#endif
886
887#else
888 (void)arch;
889#endif
890
891 default:
892 operand = opcode & 0x3F;
893 switch (opcode & 0xC0) {
894 case DW_CFA_offset:
895 reg = operand;
896 if (reg > kMaxRegisterNumber) {
897 _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
898 ") out of range",
899 reg);
900 return false;
901 }
902 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
903 cieInfo.dataAlignFactor;
904 results->setRegister(reg, kRegisterInCFA, offset, initialState);
905 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
906 operand, offset);
907 break;
908 case DW_CFA_advance_loc:
909 codeOffset += operand * cieInfo.codeAlignFactor;
910 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
911 static_cast<uint64_t>(codeOffset));
912 break;
913 case DW_CFA_restore:
914 reg = operand;
915 if (reg > kMaxRegisterNumber) {
916 _LIBUNWIND_LOG(
917 "malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
918 ") out of range",
919 reg);
920 return false;
921 }
922 results->restoreRegisterToInitialState(reg, initialState);
923 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
924 static_cast<uint64_t>(operand));
925 break;
926 default:
927 _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
928 return false;
929 }
930 }
931 }
932 }
933 return true;
934}
935
936} // namespace libunwind
937
938#endif // __DWARF_PARSER_HPP__
939