blob: 2153a71c2ec028bce99e5f25e64ed46dbe0a1443 [file] [log] [blame]
Louis Dionne7f068e52021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Saleem Abdulrasool17552662015-04-24 19:39:17 +00002//
Chandler Carruth61860a52019-01-19 10:56:40 +00003// 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
Saleem Abdulrasool17552662015-04-24 19:39:17 +00006//
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
Saleem Abdulrasool17552662015-04-24 19:39:17 +000020#include "libunwind.h"
21#include "dwarf2.h"
Daniel Cederman9f2f07a2019-01-14 10:15:20 +000022#include "Registers.hpp"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000023
Saleem Abdulrasool286a9162017-01-25 02:27:45 +000024#include "config.h"
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +000025
Saleem Abdulrasool17552662015-04-24 19:39:17 +000026namespace libunwind {
27
28/// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
Ed Maste4c43c3d2016-07-19 17:15:50 +000029/// See DWARF Spec for details:
Saleem Abdulrasool17552662015-04-24 19:39:17 +000030/// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
31///
32template <typename A>
33class CFI_Parser {
34public:
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;
Luke Cheesemancba83c32018-12-17 11:43:24 +000052#if defined(_LIBUNWIND_TARGET_AARCH64)
53 bool addressesSignedWithBKey;
54#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +000055 };
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 {
Ed Maste567984c2016-07-20 15:19:09 +000068 kMaxRegisterNumber = _LIBUNWIND_HIGHEST_DWARF_REGISTER
Saleem Abdulrasool17552662015-04-24 19:39:17 +000069 };
70 enum RegisterSavedWhere {
71 kRegisterUnused,
Daniel Kiss163101b2020-09-16 23:03:19 +020072 kRegisterUndefined,
Saleem Abdulrasool17552662015-04-24 19:39:17 +000073 kRegisterInCFA,
74 kRegisterOffsetFromCFA,
75 kRegisterInRegister,
76 kRegisterAtExpression,
77 kRegisterIsExpression
78 };
79 struct RegisterLocation {
80 RegisterSavedWhere location;
Sterling Augustinec0ef0db2020-03-04 16:29:58 -080081 bool initialStateSaved;
Saleem Abdulrasool17552662015-04-24 19:39:17 +000082 int64_t value;
83 };
84 /// Information about a frame layout and registers saved determined
Ed Maste4c43c3d2016-07-19 17:15:50 +000085 /// by "running" the DWARF FDE "instructions"
Saleem Abdulrasool17552662015-04-24 19:39:17 +000086 struct PrologInfo {
87 uint32_t cfaRegister;
88 int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset
89 int64_t cfaExpression; // CFA = expression
90 uint32_t spExtraArgSize;
Martin Storsjof10f3c92017-10-27 07:59:01 +000091 RegisterLocation savedRegisters[kMaxRegisterNumber + 1];
Sterling Augustinec0ef0db2020-03-04 16:29:58 -080092 enum class InitializeTime { kLazy, kNormal };
93
94 // When saving registers, this data structure is lazily initialized.
95 PrologInfo(InitializeTime IT = InitializeTime::kNormal) {
96 if (IT == InitializeTime::kNormal)
97 memset(this, 0, sizeof(*this));
98 }
99 void checkSaveRegister(uint64_t reg, PrologInfo &initialState) {
100 if (!savedRegisters[reg].initialStateSaved) {
101 initialState.savedRegisters[reg] = savedRegisters[reg];
102 savedRegisters[reg].initialStateSaved = true;
103 }
104 }
105 void setRegister(uint64_t reg, RegisterSavedWhere newLocation,
106 int64_t newValue, PrologInfo &initialState) {
107 checkSaveRegister(reg, initialState);
108 savedRegisters[reg].location = newLocation;
109 savedRegisters[reg].value = newValue;
110 }
111 void setRegisterLocation(uint64_t reg, RegisterSavedWhere newLocation,
112 PrologInfo &initialState) {
113 checkSaveRegister(reg, initialState);
114 savedRegisters[reg].location = newLocation;
115 }
116 void setRegisterValue(uint64_t reg, int64_t newValue,
117 PrologInfo &initialState) {
118 checkSaveRegister(reg, initialState);
119 savedRegisters[reg].value = newValue;
120 }
121 void restoreRegisterToInitialState(uint64_t reg, PrologInfo &initialState) {
122 if (savedRegisters[reg].initialStateSaved)
123 savedRegisters[reg] = initialState.savedRegisters[reg];
124 // else the register still holds its initial state
125 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000126 };
127
128 struct PrologInfoStackEntry {
129 PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
130 : next(n), info(i) {}
131 PrologInfoStackEntry *next;
132 PrologInfo info;
133 };
134
Daniel Kiss6f670c82020-10-30 17:42:23 +0100135 struct RememberStack {
136 PrologInfoStackEntry *entry;
137 RememberStack() : entry(nullptr) {}
138 ~RememberStack() {
139#if defined(_LIBUNWIND_REMEMBER_CLEANUP_NEEDED)
140 // Clean up rememberStack. Even in the case where every
141 // DW_CFA_remember_state is paired with a DW_CFA_restore_state,
142 // parseInstructions can skip restore opcodes if it reaches the target PC
143 // and stops interpreting, so we have to make sure we don't leak memory.
144 while (entry) {
145 PrologInfoStackEntry *next = entry->next;
146 _LIBUNWIND_REMEMBER_FREE(entry);
147 entry = next;
148 }
149#endif
150 }
151 };
152
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000153 static bool findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
Ryan Prichard0cff8572020-09-16 01:22:55 -0700154 uintptr_t sectionLength, pint_t fdeHint, FDE_Info *fdeInfo,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000155 CIE_Info *cieInfo);
156 static const char *decodeFDE(A &addressSpace, pint_t fdeStart,
Peter S. Housel038090f2021-10-14 13:31:05 -0700157 FDE_Info *fdeInfo, CIE_Info *cieInfo,
158 bool useCIEInfo = false);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000159 static bool parseFDEInstructions(A &addressSpace, const FDE_Info &fdeInfo,
160 const CIE_Info &cieInfo, pint_t upToPC,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000161 int arch, PrologInfo *results);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000162
163 static const char *parseCIE(A &addressSpace, pint_t cie, CIE_Info *cieInfo);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000164};
165
Peter S. Housel038090f2021-10-14 13:31:05 -0700166/// Parse a FDE into a CIE_Info and an FDE_Info. If useCIEInfo is
167/// true, treat cieInfo as already-parsed CIE_Info (whose start offset
168/// must match the one specified by the FDE) rather than parsing the
169/// one indicated within the FDE.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000170template <typename A>
171const char *CFI_Parser<A>::decodeFDE(A &addressSpace, pint_t fdeStart,
Peter S. Housel038090f2021-10-14 13:31:05 -0700172 FDE_Info *fdeInfo, CIE_Info *cieInfo,
173 bool useCIEInfo) {
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000174 pint_t p = fdeStart;
175 pint_t cfiLength = (pint_t)addressSpace.get32(p);
176 p += 4;
177 if (cfiLength == 0xffffffff) {
178 // 0xffffffff means length is really next 8 bytes
179 cfiLength = (pint_t)addressSpace.get64(p);
180 p += 8;
181 }
182 if (cfiLength == 0)
Ryan Prichard0cff8572020-09-16 01:22:55 -0700183 return "FDE has zero length"; // zero terminator
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000184 uint32_t ciePointer = addressSpace.get32(p);
185 if (ciePointer == 0)
186 return "FDE is really a CIE"; // this is a CIE not an FDE
187 pint_t nextCFI = p + cfiLength;
188 pint_t cieStart = p - ciePointer;
Peter S. Housel038090f2021-10-14 13:31:05 -0700189 if (useCIEInfo) {
190 if (cieInfo->cieStart != cieStart)
191 return "CIE start does not match";
192 } else {
193 const char *err = parseCIE(addressSpace, cieStart, cieInfo);
194 if (err != NULL)
195 return err;
196 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000197 p += 4;
Ed Maste1b651132016-07-19 17:28:38 +0000198 // Parse pc begin and range.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000199 pint_t pcStart =
200 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
201 pint_t pcRange =
202 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding & 0x0F);
Ed Maste1b651132016-07-19 17:28:38 +0000203 // Parse rest of info.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000204 fdeInfo->lsda = 0;
Ed Maste1b651132016-07-19 17:28:38 +0000205 // Check for augmentation length.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000206 if (cieInfo->fdesHaveAugmentationData) {
207 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
208 pint_t endOfAug = p + augLen;
209 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
Ed Maste1b651132016-07-19 17:28:38 +0000210 // Peek at value (without indirection). Zero means no LSDA.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000211 pint_t lsdaStart = p;
212 if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F) !=
213 0) {
Ed Maste1b651132016-07-19 17:28:38 +0000214 // Reset pointer and re-parse LSDA address.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000215 p = lsdaStart;
216 fdeInfo->lsda =
217 addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
218 }
219 }
220 p = endOfAug;
221 }
222 fdeInfo->fdeStart = fdeStart;
223 fdeInfo->fdeLength = nextCFI - fdeStart;
224 fdeInfo->fdeInstructions = p;
225 fdeInfo->pcStart = pcStart;
226 fdeInfo->pcEnd = pcStart + pcRange;
227 return NULL; // success
228}
229
230/// Scan an eh_frame section to find an FDE for a pc
231template <typename A>
232bool CFI_Parser<A>::findFDE(A &addressSpace, pint_t pc, pint_t ehSectionStart,
Ryan Prichard0cff8572020-09-16 01:22:55 -0700233 uintptr_t sectionLength, pint_t fdeHint,
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000234 FDE_Info *fdeInfo, CIE_Info *cieInfo) {
235 //fprintf(stderr, "findFDE(0x%llX)\n", (long long)pc);
236 pint_t p = (fdeHint != 0) ? fdeHint : ehSectionStart;
Ryan Prichard0cff8572020-09-16 01:22:55 -0700237 const pint_t ehSectionEnd = (sectionLength == UINTPTR_MAX)
238 ? static_cast<pint_t>(-1)
239 : (ehSectionStart + sectionLength);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000240 while (p < ehSectionEnd) {
241 pint_t currentCFI = p;
242 //fprintf(stderr, "findFDE() CFI at 0x%llX\n", (long long)p);
243 pint_t cfiLength = addressSpace.get32(p);
244 p += 4;
245 if (cfiLength == 0xffffffff) {
246 // 0xffffffff means length is really next 8 bytes
247 cfiLength = (pint_t)addressSpace.get64(p);
248 p += 8;
249 }
250 if (cfiLength == 0)
Ryan Prichard0cff8572020-09-16 01:22:55 -0700251 return false; // zero terminator
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000252 uint32_t id = addressSpace.get32(p);
253 if (id == 0) {
Ed Maste1b651132016-07-19 17:28:38 +0000254 // Skip over CIEs.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000255 p += cfiLength;
256 } else {
Ed Maste1b651132016-07-19 17:28:38 +0000257 // Process FDE to see if it covers pc.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000258 pint_t nextCFI = p + cfiLength;
259 uint32_t ciePointer = addressSpace.get32(p);
260 pint_t cieStart = p - ciePointer;
Ed Maste1b651132016-07-19 17:28:38 +0000261 // Validate pointer to CIE is within section.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000262 if ((ehSectionStart <= cieStart) && (cieStart < ehSectionEnd)) {
263 if (parseCIE(addressSpace, cieStart, cieInfo) == NULL) {
264 p += 4;
Ed Maste1b651132016-07-19 17:28:38 +0000265 // Parse pc begin and range.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000266 pint_t pcStart =
267 addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding);
268 pint_t pcRange = addressSpace.getEncodedP(
269 p, nextCFI, cieInfo->pointerEncoding & 0x0F);
Ed Maste1b651132016-07-19 17:28:38 +0000270 // Test if pc is within the function this FDE covers.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000271 if ((pcStart < pc) && (pc <= pcStart + pcRange)) {
272 // parse rest of info
273 fdeInfo->lsda = 0;
274 // check for augmentation length
275 if (cieInfo->fdesHaveAugmentationData) {
276 pint_t augLen = (pint_t)addressSpace.getULEB128(p, nextCFI);
277 pint_t endOfAug = p + augLen;
278 if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
Ed Maste1b651132016-07-19 17:28:38 +0000279 // Peek at value (without indirection). Zero means no LSDA.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000280 pint_t lsdaStart = p;
281 if (addressSpace.getEncodedP(
282 p, nextCFI, cieInfo->lsdaEncoding & 0x0F) != 0) {
Ed Maste1b651132016-07-19 17:28:38 +0000283 // Reset pointer and re-parse LSDA address.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000284 p = lsdaStart;
285 fdeInfo->lsda = addressSpace
286 .getEncodedP(p, nextCFI, cieInfo->lsdaEncoding);
287 }
288 }
289 p = endOfAug;
290 }
291 fdeInfo->fdeStart = currentCFI;
292 fdeInfo->fdeLength = nextCFI - currentCFI;
293 fdeInfo->fdeInstructions = p;
294 fdeInfo->pcStart = pcStart;
295 fdeInfo->pcEnd = pcStart + pcRange;
296 return true;
297 } else {
298 // pc is not in begin/range, skip this FDE
299 }
300 } else {
Ed Maste1b651132016-07-19 17:28:38 +0000301 // Malformed CIE, now augmentation describing pc range encoding.
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000302 }
303 } else {
304 // malformed FDE. CIE is bad
305 }
306 p = nextCFI;
307 }
308 }
309 return false;
310}
311
312/// Extract info from a CIE
313template <typename A>
314const char *CFI_Parser<A>::parseCIE(A &addressSpace, pint_t cie,
315 CIE_Info *cieInfo) {
316 cieInfo->pointerEncoding = 0;
317 cieInfo->lsdaEncoding = DW_EH_PE_omit;
318 cieInfo->personalityEncoding = 0;
319 cieInfo->personalityOffsetInCIE = 0;
320 cieInfo->personality = 0;
321 cieInfo->codeAlignFactor = 0;
322 cieInfo->dataAlignFactor = 0;
323 cieInfo->isSignalFrame = false;
324 cieInfo->fdesHaveAugmentationData = false;
Luke Cheesemancba83c32018-12-17 11:43:24 +0000325#if defined(_LIBUNWIND_TARGET_AARCH64)
326 cieInfo->addressesSignedWithBKey = false;
327#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000328 cieInfo->cieStart = cie;
329 pint_t p = cie;
330 pint_t cieLength = (pint_t)addressSpace.get32(p);
331 p += 4;
332 pint_t cieContentEnd = p + cieLength;
333 if (cieLength == 0xffffffff) {
334 // 0xffffffff means length is really next 8 bytes
335 cieLength = (pint_t)addressSpace.get64(p);
336 p += 8;
337 cieContentEnd = p + cieLength;
338 }
339 if (cieLength == 0)
340 return NULL;
341 // CIE ID is always 0
342 if (addressSpace.get32(p) != 0)
343 return "CIE ID is not zero";
344 p += 4;
345 // Version is always 1 or 3
346 uint8_t version = addressSpace.get8(p);
347 if ((version != 1) && (version != 3))
348 return "CIE version is not 1 or 3";
349 ++p;
350 // save start of augmentation string and find end
351 pint_t strStart = p;
352 while (addressSpace.get8(p) != 0)
353 ++p;
354 ++p;
355 // parse code aligment factor
356 cieInfo->codeAlignFactor = (uint32_t)addressSpace.getULEB128(p, cieContentEnd);
357 // parse data alignment factor
358 cieInfo->dataAlignFactor = (int)addressSpace.getSLEB128(p, cieContentEnd);
359 // parse return address register
Ryan Prichard58b19e62020-07-13 22:06:22 -0700360 uint64_t raReg = (version == 1) ? addressSpace.get8(p++)
361 : addressSpace.getULEB128(p, cieContentEnd);
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000362 assert(raReg < 255 && "return address register too large");
363 cieInfo->returnAddressRegister = (uint8_t)raReg;
364 // parse augmentation data based on augmentation string
365 const char *result = NULL;
366 if (addressSpace.get8(strStart) == 'z') {
367 // parse augmentation data length
368 addressSpace.getULEB128(p, cieContentEnd);
369 for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
370 switch (addressSpace.get8(s)) {
371 case 'z':
372 cieInfo->fdesHaveAugmentationData = true;
373 break;
374 case 'P':
375 cieInfo->personalityEncoding = addressSpace.get8(p);
376 ++p;
377 cieInfo->personalityOffsetInCIE = (uint8_t)(p - cie);
378 cieInfo->personality = addressSpace
379 .getEncodedP(p, cieContentEnd, cieInfo->personalityEncoding);
380 break;
381 case 'L':
382 cieInfo->lsdaEncoding = addressSpace.get8(p);
383 ++p;
384 break;
385 case 'R':
386 cieInfo->pointerEncoding = addressSpace.get8(p);
387 ++p;
388 break;
389 case 'S':
390 cieInfo->isSignalFrame = true;
391 break;
Luke Cheesemancba83c32018-12-17 11:43:24 +0000392#if defined(_LIBUNWIND_TARGET_AARCH64)
393 case 'B':
394 cieInfo->addressesSignedWithBKey = true;
395 break;
396#endif
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000397 default:
398 // ignore unknown letters
399 break;
400 }
401 }
402 }
403 cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
404 cieInfo->cieInstructions = p;
405 return result;
406}
407
408
Ed Maste4c43c3d2016-07-19 17:15:50 +0000409/// "run" the DWARF instructions and create the abstact PrologInfo for an FDE
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000410template <typename A>
411bool CFI_Parser<A>::parseFDEInstructions(A &addressSpace,
412 const FDE_Info &fdeInfo,
413 const CIE_Info &cieInfo, pint_t upToPC,
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000414 int arch, PrologInfo *results) {
Daniel Kiss6f670c82020-10-30 17:42:23 +0100415 // Alloca is used for the allocation of the rememberStack entries. It removes
416 // the dependency on new/malloc but the below for loop can not be refactored
417 // into functions. Entry could be saved during the processing of a CIE and
418 // restored by an FDE.
419 RememberStack rememberStack;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000420
Daniel Kiss6f670c82020-10-30 17:42:23 +0100421 struct ParseInfo {
422 pint_t instructions;
423 pint_t instructionsEnd;
424 pint_t pcoffset;
425 };
Jorge Gorbe Moyafd8399e2020-02-18 11:48:02 -0800426
Daniel Kiss6f670c82020-10-30 17:42:23 +0100427 ParseInfo parseInfoArray[] = {
428 {cieInfo.cieInstructions, cieInfo.cieStart + cieInfo.cieLength,
429 (pint_t)(-1)},
430 {fdeInfo.fdeInstructions, fdeInfo.fdeStart + fdeInfo.fdeLength,
431 upToPC - fdeInfo.pcStart}};
Jorge Gorbe Moyafd8399e2020-02-18 11:48:02 -0800432
Daniel Kiss6f670c82020-10-30 17:42:23 +0100433 for (const auto &info : parseInfoArray) {
434 pint_t p = info.instructions;
435 pint_t instructionsEnd = info.instructionsEnd;
436 pint_t pcoffset = info.pcoffset;
437 pint_t codeOffset = 0;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000438
Daniel Kiss6f670c82020-10-30 17:42:23 +0100439 // initialState initialized as registers in results are modified. Use
440 // PrologInfo accessor functions to avoid reading uninitialized data.
441 PrologInfo initialState(PrologInfo::InitializeTime::kLazy);
Saleem Abdulrasoold1be5b02017-01-21 16:22:57 +0000442
Daniel Kiss6f670c82020-10-30 17:42:23 +0100443 _LIBUNWIND_TRACE_DWARF("parseFDEInstructions(instructions=0x%0" PRIx64
444 ")\n",
445 static_cast<uint64_t>(instructionsEnd));
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000446
Daniel Kiss6f670c82020-10-30 17:42:23 +0100447 // see DWARF Spec, section 6.4.2 for details on unwind opcodes
448 while ((p < instructionsEnd) && (codeOffset < pcoffset)) {
449 uint64_t reg;
450 uint64_t reg2;
451 int64_t offset;
452 uint64_t length;
453 uint8_t opcode = addressSpace.get8(p);
454 uint8_t operand;
455
456 ++p;
457 switch (opcode) {
458 case DW_CFA_nop:
459 _LIBUNWIND_TRACE_DWARF("DW_CFA_nop\n");
460 break;
461 case DW_CFA_set_loc:
462 codeOffset = addressSpace.getEncodedP(p, instructionsEnd,
463 cieInfo.pointerEncoding);
464 _LIBUNWIND_TRACE_DWARF("DW_CFA_set_loc\n");
465 break;
466 case DW_CFA_advance_loc1:
467 codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
468 p += 1;
469 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc1: new offset=%" PRIu64 "\n",
470 static_cast<uint64_t>(codeOffset));
471 break;
472 case DW_CFA_advance_loc2:
473 codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
474 p += 2;
475 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc2: new offset=%" PRIu64 "\n",
476 static_cast<uint64_t>(codeOffset));
477 break;
478 case DW_CFA_advance_loc4:
479 codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
480 p += 4;
481 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc4: new offset=%" PRIu64 "\n",
482 static_cast<uint64_t>(codeOffset));
483 break;
484 case DW_CFA_offset_extended:
485 reg = addressSpace.getULEB128(p, instructionsEnd);
486 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
487 cieInfo.dataAlignFactor;
488 if (reg > kMaxRegisterNumber) {
489 _LIBUNWIND_LOG0(
490 "malformed DW_CFA_offset_extended DWARF unwind, reg too big");
491 return false;
492 }
493 results->setRegister(reg, kRegisterInCFA, offset, initialState);
494 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended(reg=%" PRIu64 ", "
495 "offset=%" PRId64 ")\n",
496 reg, offset);
497 break;
498 case DW_CFA_restore_extended:
499 reg = addressSpace.getULEB128(p, instructionsEnd);
500 if (reg > kMaxRegisterNumber) {
501 _LIBUNWIND_LOG0(
502 "malformed DW_CFA_restore_extended DWARF unwind, reg too big");
503 return false;
504 }
505 results->restoreRegisterToInitialState(reg, initialState);
506 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_extended(reg=%" PRIu64 ")\n",
507 reg);
508 break;
509 case DW_CFA_undefined:
510 reg = addressSpace.getULEB128(p, instructionsEnd);
511 if (reg > kMaxRegisterNumber) {
512 _LIBUNWIND_LOG0(
513 "malformed DW_CFA_undefined DWARF unwind, reg too big");
514 return false;
515 }
516 results->setRegisterLocation(reg, kRegisterUndefined, initialState);
517 _LIBUNWIND_TRACE_DWARF("DW_CFA_undefined(reg=%" PRIu64 ")\n", reg);
518 break;
519 case DW_CFA_same_value:
520 reg = addressSpace.getULEB128(p, instructionsEnd);
521 if (reg > kMaxRegisterNumber) {
522 _LIBUNWIND_LOG0(
523 "malformed DW_CFA_same_value DWARF unwind, reg too big");
524 return false;
525 }
526 // <rdar://problem/8456377> DW_CFA_same_value unsupported
527 // "same value" means register was stored in frame, but its current
528 // value has not changed, so no need to restore from frame.
529 // We model this as if the register was never saved.
530 results->setRegisterLocation(reg, kRegisterUnused, initialState);
Daniel Kiss6f670c82020-10-30 17:42:23 +0100531 _LIBUNWIND_TRACE_DWARF("DW_CFA_same_value(reg=%" PRIu64 ")\n", reg);
532 break;
533 case DW_CFA_register:
534 reg = addressSpace.getULEB128(p, instructionsEnd);
535 reg2 = addressSpace.getULEB128(p, instructionsEnd);
536 if (reg > kMaxRegisterNumber) {
537 _LIBUNWIND_LOG0(
538 "malformed DW_CFA_register DWARF unwind, reg too big");
539 return false;
540 }
541 if (reg2 > kMaxRegisterNumber) {
542 _LIBUNWIND_LOG0(
543 "malformed DW_CFA_register DWARF unwind, reg2 too big");
544 return false;
545 }
546 results->setRegister(reg, kRegisterInRegister, (int64_t)reg2,
547 initialState);
Daniel Kiss6f670c82020-10-30 17:42:23 +0100548 _LIBUNWIND_TRACE_DWARF(
549 "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n", reg, reg2);
550 break;
551 case DW_CFA_remember_state: {
552 // Avoid operator new because that would be an upward dependency.
553 // Avoid malloc because it needs heap allocation.
554 PrologInfoStackEntry *entry =
555 (PrologInfoStackEntry *)_LIBUNWIND_REMEMBER_ALLOC(
556 sizeof(PrologInfoStackEntry));
557 if (entry != NULL) {
558 entry->next = rememberStack.entry;
559 entry->info = *results;
560 rememberStack.entry = entry;
561 } else {
562 return false;
563 }
564 _LIBUNWIND_TRACE_DWARF("DW_CFA_remember_state\n");
565 break;
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000566 }
Daniel Kiss6f670c82020-10-30 17:42:23 +0100567 case DW_CFA_restore_state:
568 if (rememberStack.entry != NULL) {
569 PrologInfoStackEntry *top = rememberStack.entry;
570 *results = top->info;
571 rememberStack.entry = top->next;
572 _LIBUNWIND_REMEMBER_FREE(top);
573 } else {
574 return false;
575 }
576 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore_state\n");
577 break;
578 case DW_CFA_def_cfa:
579 reg = addressSpace.getULEB128(p, instructionsEnd);
580 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);
581 if (reg > kMaxRegisterNumber) {
582 _LIBUNWIND_LOG0("malformed DW_CFA_def_cfa DWARF unwind, reg too big");
583 return false;
584 }
585 results->cfaRegister = (uint32_t)reg;
586 results->cfaRegisterOffset = (int32_t)offset;
587 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa(reg=%" PRIu64 ", offset=%" PRIu64
588 ")\n",
589 reg, offset);
590 break;
591 case DW_CFA_def_cfa_register:
592 reg = addressSpace.getULEB128(p, instructionsEnd);
593 if (reg > kMaxRegisterNumber) {
594 _LIBUNWIND_LOG0(
595 "malformed DW_CFA_def_cfa_register DWARF unwind, reg too big");
596 return false;
597 }
598 results->cfaRegister = (uint32_t)reg;
599 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_register(%" PRIu64 ")\n", reg);
600 break;
601 case DW_CFA_def_cfa_offset:
602 results->cfaRegisterOffset =
603 (int32_t)addressSpace.getULEB128(p, instructionsEnd);
Daniel Kiss6f670c82020-10-30 17:42:23 +0100604 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset(%d)\n",
605 results->cfaRegisterOffset);
606 break;
607 case DW_CFA_def_cfa_expression:
608 results->cfaRegister = 0;
609 results->cfaExpression = (int64_t)p;
610 length = addressSpace.getULEB128(p, instructionsEnd);
611 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
612 p += static_cast<pint_t>(length);
613 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_expression(expression=0x%" PRIx64
614 ", length=%" PRIu64 ")\n",
615 results->cfaExpression, length);
616 break;
617 case DW_CFA_expression:
618 reg = addressSpace.getULEB128(p, instructionsEnd);
619 if (reg > kMaxRegisterNumber) {
620 _LIBUNWIND_LOG0(
621 "malformed DW_CFA_expression DWARF unwind, reg too big");
622 return false;
623 }
624 results->setRegister(reg, kRegisterAtExpression, (int64_t)p,
625 initialState);
626 length = addressSpace.getULEB128(p, instructionsEnd);
627 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
628 p += static_cast<pint_t>(length);
629 _LIBUNWIND_TRACE_DWARF("DW_CFA_expression(reg=%" PRIu64 ", "
630 "expression=0x%" PRIx64 ", "
631 "length=%" PRIu64 ")\n",
632 reg, results->savedRegisters[reg].value, length);
633 break;
634 case DW_CFA_offset_extended_sf:
635 reg = addressSpace.getULEB128(p, instructionsEnd);
636 if (reg > kMaxRegisterNumber) {
637 _LIBUNWIND_LOG0(
638 "malformed DW_CFA_offset_extended_sf DWARF unwind, reg too big");
639 return false;
640 }
641 offset = addressSpace.getSLEB128(p, instructionsEnd) *
642 cieInfo.dataAlignFactor;
643 results->setRegister(reg, kRegisterInCFA, offset, initialState);
644 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset_extended_sf(reg=%" PRIu64 ", "
645 "offset=%" PRId64 ")\n",
646 reg, offset);
647 break;
648 case DW_CFA_def_cfa_sf:
649 reg = addressSpace.getULEB128(p, instructionsEnd);
650 offset = addressSpace.getSLEB128(p, instructionsEnd) *
651 cieInfo.dataAlignFactor;
652 if (reg > kMaxRegisterNumber) {
653 _LIBUNWIND_LOG0(
654 "malformed DW_CFA_def_cfa_sf DWARF unwind, reg too big");
655 return false;
656 }
657 results->cfaRegister = (uint32_t)reg;
658 results->cfaRegisterOffset = (int32_t)offset;
659 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_sf(reg=%" PRIu64 ", "
660 "offset=%" PRId64 ")\n",
661 reg, offset);
662 break;
663 case DW_CFA_def_cfa_offset_sf:
664 results->cfaRegisterOffset =
665 (int32_t)(addressSpace.getSLEB128(p, instructionsEnd) *
666 cieInfo.dataAlignFactor);
Daniel Kiss6f670c82020-10-30 17:42:23 +0100667 _LIBUNWIND_TRACE_DWARF("DW_CFA_def_cfa_offset_sf(%d)\n",
668 results->cfaRegisterOffset);
669 break;
670 case DW_CFA_val_offset:
671 reg = addressSpace.getULEB128(p, instructionsEnd);
672 if (reg > kMaxRegisterNumber) {
673 _LIBUNWIND_LOG(
674 "malformed DW_CFA_val_offset DWARF unwind, reg (%" PRIu64
675 ") out of range\n",
676 reg);
677 return false;
678 }
679 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
680 cieInfo.dataAlignFactor;
681 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
682 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset(reg=%" PRIu64 ", "
683 "offset=%" PRId64 "\n",
684 reg, offset);
685 break;
686 case DW_CFA_val_offset_sf:
687 reg = addressSpace.getULEB128(p, instructionsEnd);
688 if (reg > kMaxRegisterNumber) {
689 _LIBUNWIND_LOG0(
690 "malformed DW_CFA_val_offset_sf DWARF unwind, reg too big");
691 return false;
692 }
693 offset = addressSpace.getSLEB128(p, instructionsEnd) *
694 cieInfo.dataAlignFactor;
695 results->setRegister(reg, kRegisterOffsetFromCFA, offset, initialState);
696 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_offset_sf(reg=%" PRIu64 ", "
697 "offset=%" PRId64 "\n",
698 reg, offset);
699 break;
700 case DW_CFA_val_expression:
701 reg = addressSpace.getULEB128(p, instructionsEnd);
702 if (reg > kMaxRegisterNumber) {
703 _LIBUNWIND_LOG0(
704 "malformed DW_CFA_val_expression DWARF unwind, reg too big");
705 return false;
706 }
707 results->setRegister(reg, kRegisterIsExpression, (int64_t)p,
708 initialState);
709 length = addressSpace.getULEB128(p, instructionsEnd);
710 assert(length < static_cast<pint_t>(~0) && "pointer overflow");
711 p += static_cast<pint_t>(length);
712 _LIBUNWIND_TRACE_DWARF("DW_CFA_val_expression(reg=%" PRIu64 ", "
713 "expression=0x%" PRIx64 ", length=%" PRIu64
714 ")\n",
715 reg, results->savedRegisters[reg].value, length);
716 break;
717 case DW_CFA_GNU_args_size:
718 length = addressSpace.getULEB128(p, instructionsEnd);
719 results->spExtraArgSize = (uint32_t)length;
720 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_args_size(%" PRIu64 ")\n", length);
721 break;
722 case DW_CFA_GNU_negative_offset_extended:
723 reg = addressSpace.getULEB128(p, instructionsEnd);
724 if (reg > kMaxRegisterNumber) {
725 _LIBUNWIND_LOG0("malformed DW_CFA_GNU_negative_offset_extended DWARF "
726 "unwind, reg too big");
727 return false;
728 }
729 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
730 cieInfo.dataAlignFactor;
731 results->setRegister(reg, kRegisterInCFA, -offset, initialState);
732 _LIBUNWIND_TRACE_DWARF(
733 "DW_CFA_GNU_negative_offset_extended(%" PRId64 ")\n", offset);
734 break;
Luke Cheeseman4d8e4312018-12-14 11:30:12 +0000735
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000736#if defined(_LIBUNWIND_TARGET_AARCH64) || defined(_LIBUNWIND_TARGET_SPARC)
Daniel Kiss6f670c82020-10-30 17:42:23 +0100737 // The same constant is used to represent different instructions on
738 // AArch64 (negate_ra_state) and SPARC (window_save).
739 static_assert(DW_CFA_AARCH64_negate_ra_state == DW_CFA_GNU_window_save,
740 "uses the same constant");
741 case DW_CFA_AARCH64_negate_ra_state:
742 switch (arch) {
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000743#if defined(_LIBUNWIND_TARGET_AARCH64)
Sterling Augustinec0ef0db2020-03-04 16:29:58 -0800744 case REGISTERS_ARM64: {
745 int64_t value =
Fangrui Song5f263002021-08-20 14:26:27 -0700746 results->savedRegisters[UNW_AARCH64_RA_SIGN_STATE].value ^ 0x1;
747 results->setRegisterValue(UNW_AARCH64_RA_SIGN_STATE, value,
Daniel Kiss6f670c82020-10-30 17:42:23 +0100748 initialState);
Sterling Augustinec0ef0db2020-03-04 16:29:58 -0800749 _LIBUNWIND_TRACE_DWARF("DW_CFA_AARCH64_negate_ra_state\n");
750 } break;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000751#endif
Sterling Augustinec0ef0db2020-03-04 16:29:58 -0800752
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000753#if defined(_LIBUNWIND_TARGET_SPARC)
Daniel Kiss6f670c82020-10-30 17:42:23 +0100754 // case DW_CFA_GNU_window_save:
755 case REGISTERS_SPARC:
756 _LIBUNWIND_TRACE_DWARF("DW_CFA_GNU_window_save()\n");
757 for (reg = UNW_SPARC_O0; reg <= UNW_SPARC_O7; reg++) {
758 results->setRegister(reg, kRegisterInRegister,
759 ((int64_t)reg - UNW_SPARC_O0) + UNW_SPARC_I0,
760 initialState);
761 }
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000762
Daniel Kiss6f670c82020-10-30 17:42:23 +0100763 for (reg = UNW_SPARC_L0; reg <= UNW_SPARC_I7; reg++) {
764 results->setRegister(reg, kRegisterInCFA,
765 ((int64_t)reg - UNW_SPARC_L0) * 4,
766 initialState);
767 }
768 break;
769#endif
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000770 }
771 break;
Daniel Cederman9f2f07a2019-01-14 10:15:20 +0000772#else
Daniel Kiss6f670c82020-10-30 17:42:23 +0100773 (void)arch;
Luke Cheeseman4d8e4312018-12-14 11:30:12 +0000774#endif
775
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000776 default:
Daniel Kiss6f670c82020-10-30 17:42:23 +0100777 operand = opcode & 0x3F;
778 switch (opcode & 0xC0) {
779 case DW_CFA_offset:
780 reg = operand;
781 if (reg > kMaxRegisterNumber) {
782 _LIBUNWIND_LOG("malformed DW_CFA_offset DWARF unwind, reg (%" PRIu64
783 ") out of range",
784 reg);
785 return false;
786 }
787 offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd) *
788 cieInfo.dataAlignFactor;
789 results->setRegister(reg, kRegisterInCFA, offset, initialState);
790 _LIBUNWIND_TRACE_DWARF("DW_CFA_offset(reg=%d, offset=%" PRId64 ")\n",
791 operand, offset);
792 break;
793 case DW_CFA_advance_loc:
794 codeOffset += operand * cieInfo.codeAlignFactor;
795 _LIBUNWIND_TRACE_DWARF("DW_CFA_advance_loc: new offset=%" PRIu64 "\n",
796 static_cast<uint64_t>(codeOffset));
797 break;
798 case DW_CFA_restore:
799 reg = operand;
800 if (reg > kMaxRegisterNumber) {
801 _LIBUNWIND_LOG(
802 "malformed DW_CFA_restore DWARF unwind, reg (%" PRIu64
803 ") out of range",
804 reg);
805 return false;
806 }
807 results->restoreRegisterToInitialState(reg, initialState);
808 _LIBUNWIND_TRACE_DWARF("DW_CFA_restore(reg=%" PRIu64 ")\n",
809 static_cast<uint64_t>(operand));
810 break;
811 default:
812 _LIBUNWIND_TRACE_DWARF("unknown CFA opcode 0x%02X\n", opcode);
813 return false;
814 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000815 }
816 }
817 }
Saleem Abdulrasool17552662015-04-24 19:39:17 +0000818 return true;
819}
820
821} // namespace libunwind
822
823#endif // __DWARF_PARSER_HPP__