DwarfParser.hpp revision 1.5.2.2 1 1.5.2.2 rmind //===--------------------------- DwarfParser.hpp --------------------------===//
2 1.5.2.2 rmind //
3 1.5.2.2 rmind // The LLVM Compiler Infrastructure
4 1.5.2.2 rmind //
5 1.5.2.2 rmind // This file is dual licensed under the MIT and the University of Illinois Open
6 1.5.2.2 rmind // Source Licenses. See LICENSE.TXT for details.
7 1.5.2.2 rmind //
8 1.5.2.2 rmind //
9 1.5.2.2 rmind // Parses DWARF CFIs (FDEs and CIEs).
10 1.5.2.2 rmind //
11 1.5.2.2 rmind //===----------------------------------------------------------------------===//
12 1.5.2.2 rmind
13 1.5.2.2 rmind #ifndef __DWARF_PARSER_HPP__
14 1.5.2.2 rmind #define __DWARF_PARSER_HPP__
15 1.5.2.2 rmind
16 1.5.2.2 rmind #include <cstdint>
17 1.5.2.2 rmind #include <cstdlib>
18 1.5.2.2 rmind
19 1.5.2.2 rmind #include "dwarf2.h"
20 1.5.2.2 rmind #include "AddressSpace.hpp"
21 1.5.2.2 rmind
22 1.5.2.2 rmind namespace _Unwind {
23 1.5.2.2 rmind
24 1.5.2.2 rmind /// CFI_Parser does basic parsing of a CFI (Call Frame Information) records.
25 1.5.2.2 rmind /// See Dwarf Spec for details:
26 1.5.2.2 rmind /// http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
27 1.5.2.2 rmind ///
28 1.5.2.2 rmind template <typename A, typename R> class CFI_Parser {
29 1.5.2.2 rmind public:
30 1.5.2.2 rmind typedef typename A::pint_t pint_t;
31 1.5.2.2 rmind
32 1.5.2.2 rmind /// Information encoded in a CIE (Common Information Entry)
33 1.5.2.2 rmind struct CIE_Info {
34 1.5.2.2 rmind pint_t cieStart;
35 1.5.2.2 rmind pint_t cieLength;
36 1.5.2.2 rmind pint_t cieInstructions;
37 1.5.2.2 rmind pint_t personality;
38 1.5.2.2 rmind uint32_t codeAlignFactor;
39 1.5.2.2 rmind int dataAlignFactor;
40 1.5.2.2 rmind uint8_t pointerEncoding;
41 1.5.2.2 rmind uint8_t lsdaEncoding;
42 1.5.2.2 rmind uint8_t personalityEncoding;
43 1.5.2.2 rmind uint8_t personalityOffsetInCIE;
44 1.5.2.2 rmind bool isSignalFrame;
45 1.5.2.2 rmind bool fdesHaveAugmentationData;
46 1.5.2.2 rmind uint8_t returnAddressRegister;
47 1.5.2.2 rmind };
48 1.5.2.2 rmind
49 1.5.2.2 rmind /// Information about an FDE (Frame Description Entry)
50 1.5.2.2 rmind struct FDE_Info {
51 1.5.2.2 rmind pint_t fdeStart;
52 1.5.2.2 rmind pint_t fdeLength;
53 1.5.2.2 rmind pint_t fdeInstructions;
54 1.5.2.2 rmind pint_t pcStart;
55 1.5.2.2 rmind pint_t pcEnd;
56 1.5.2.2 rmind pint_t lsda;
57 1.5.2.2 rmind };
58 1.5.2.2 rmind
59 1.5.2.2 rmind /// Information about a frame layout and registers saved determined
60 1.5.2.2 rmind /// by "running" the DWARF FDE "instructions"
61 1.5.2.2 rmind enum {
62 1.5.2.2 rmind kMaxRegisterNumber = R::LAST_REGISTER + 1
63 1.5.2.2 rmind };
64 1.5.2.2 rmind enum RegisterSavedWhere {
65 1.5.2.2 rmind kRegisterUnused,
66 1.5.2.2 rmind kRegisterInCFA,
67 1.5.2.2 rmind kRegisterOffsetFromCFA,
68 1.5.2.2 rmind kRegisterInRegister,
69 1.5.2.2 rmind kRegisterAtExpression,
70 1.5.2.2 rmind kRegisterIsExpression,
71 1.5.2.2 rmind };
72 1.5.2.2 rmind struct RegisterLocation {
73 1.5.2.2 rmind RegisterSavedWhere location;
74 1.5.2.2 rmind int64_t value;
75 1.5.2.2 rmind };
76 1.5.2.2 rmind struct PrologInfo {
77 1.5.2.2 rmind uint32_t cfaRegister;
78 1.5.2.2 rmind int32_t cfaRegisterOffset; // CFA = (cfaRegister)+cfaRegisterOffset
79 1.5.2.2 rmind int64_t cfaExpression; // CFA = expression
80 1.5.2.2 rmind uint32_t spExtraArgSize;
81 1.5.2.2 rmind uint32_t codeOffsetAtStackDecrement;
82 1.5.2.2 rmind RegisterLocation savedRegisters[kMaxRegisterNumber];
83 1.5.2.2 rmind };
84 1.5.2.2 rmind
85 1.5.2.2 rmind struct PrologInfoStackEntry {
86 1.5.2.2 rmind PrologInfoStackEntry(PrologInfoStackEntry *n, const PrologInfo &i)
87 1.5.2.2 rmind : next(n), info(i) {}
88 1.5.2.2 rmind PrologInfoStackEntry *next;
89 1.5.2.2 rmind PrologInfo info;
90 1.5.2.2 rmind };
91 1.5.2.2 rmind
92 1.5.2.2 rmind static void findPCRange(A &, pint_t, pint_t &, pint_t &);
93 1.5.2.2 rmind
94 1.5.2.2 rmind static bool decodeFDE(A &, pint_t, FDE_Info *, CIE_Info *,
95 1.5.2.2 rmind unw_proc_info_t *ctx);
96 1.5.2.2 rmind static bool parseFDEInstructions(A &, const FDE_Info &, const CIE_Info &,
97 1.5.2.2 rmind pint_t, PrologInfo *, unw_proc_info_t *ctx);
98 1.5.2.2 rmind
99 1.5.2.2 rmind static bool parseCIE(A &, pint_t, CIE_Info *);
100 1.5.2.2 rmind
101 1.5.2.2 rmind private:
102 1.5.2.2 rmind static bool parseInstructions(A &, pint_t, pint_t, const CIE_Info &, pint_t,
103 1.5.2.2 rmind PrologInfoStackEntry *&, PrologInfo *,
104 1.5.2.2 rmind unw_proc_info_t *ctx);
105 1.5.2.2 rmind };
106 1.5.2.2 rmind
107 1.5.2.2 rmind ///
108 1.5.2.2 rmind /// Parse a FDE and return the last PC it covers.
109 1.5.2.2 rmind ///
110 1.5.2.2 rmind template <typename A, typename R>
111 1.5.2.2 rmind void CFI_Parser<A, R>::findPCRange(A &addressSpace, pint_t fde, pint_t &pcStart,
112 1.5.2.2 rmind pint_t &pcEnd) {
113 1.5.2.2 rmind pcStart = 0;
114 1.5.2.2 rmind pcEnd = 0;
115 1.5.2.2 rmind pint_t p = fde;
116 1.5.2.2 rmind uint64_t cfiLength = addressSpace.get32(p);
117 1.5.2.2 rmind p += 4;
118 1.5.2.2 rmind if (cfiLength == 0xffffffff) {
119 1.5.2.2 rmind // 0xffffffff means length is really the next 8 Bytes.
120 1.5.2.2 rmind cfiLength = addressSpace.get64(p);
121 1.5.2.2 rmind p += 8;
122 1.5.2.2 rmind }
123 1.5.2.2 rmind if (cfiLength == 0)
124 1.5.2.2 rmind return;
125 1.5.2.2 rmind uint32_t ciePointer = addressSpace.get32(p);
126 1.5.2.2 rmind if (ciePointer == 0)
127 1.5.2.2 rmind return;
128 1.5.2.2 rmind pint_t nextCFI = p + cfiLength;
129 1.5.2.2 rmind pint_t cieStart = p - ciePointer;
130 1.5.2.2 rmind typename CFI_Parser<A, R>::CIE_Info cieInfo;
131 1.5.2.2 rmind if (!parseCIE(addressSpace, cieStart, &cieInfo))
132 1.5.2.2 rmind return;
133 1.5.2.2 rmind p += 4;
134 1.5.2.2 rmind // Parse pc begin and range.
135 1.5.2.2 rmind pcStart = addressSpace.getEncodedP(p, nextCFI, cieInfo.pointerEncoding, NULL);
136 1.5.2.2 rmind pcEnd = pcStart + addressSpace.getEncodedP(
137 1.5.2.2 rmind p, nextCFI, cieInfo.pointerEncoding & 0x0F, NULL);
138 1.5.2.2 rmind }
139 1.5.2.2 rmind
140 1.5.2.2 rmind ///
141 1.5.2.2 rmind /// Parse a FDE into a CIE_Info and an FDE_Info
142 1.5.2.2 rmind ///
143 1.5.2.2 rmind template <typename A, typename R>
144 1.5.2.2 rmind bool CFI_Parser<A, R>::decodeFDE(A &addressSpace, pint_t fdeStart,
145 1.5.2.2 rmind FDE_Info *fdeInfo, CIE_Info *cieInfo,
146 1.5.2.2 rmind unw_proc_info_t *ctx) {
147 1.5.2.2 rmind pint_t p = fdeStart;
148 1.5.2.2 rmind uint64_t cfiLength = addressSpace.get32(p);
149 1.5.2.2 rmind p += 4;
150 1.5.2.2 rmind if (cfiLength == 0xffffffff) {
151 1.5.2.2 rmind // 0xffffffff means length is really the next 8 Bytes.
152 1.5.2.2 rmind cfiLength = addressSpace.get64(p);
153 1.5.2.2 rmind p += 8;
154 1.5.2.2 rmind }
155 1.5.2.2 rmind if (cfiLength == 0)
156 1.5.2.2 rmind return false;
157 1.5.2.2 rmind uint32_t ciePointer = addressSpace.get32(p);
158 1.5.2.2 rmind if (ciePointer == 0)
159 1.5.2.2 rmind return false;
160 1.5.2.2 rmind pint_t nextCFI = p + cfiLength;
161 1.5.2.2 rmind pint_t cieStart = p - ciePointer;
162 1.5.2.2 rmind if (!parseCIE(addressSpace, cieStart, cieInfo))
163 1.5.2.2 rmind return false;
164 1.5.2.2 rmind p += 4;
165 1.5.2.2 rmind // Parse pc begin and range.
166 1.5.2.2 rmind pint_t pcStart =
167 1.5.2.2 rmind addressSpace.getEncodedP(p, nextCFI, cieInfo->pointerEncoding, ctx);
168 1.5.2.2 rmind pint_t pcRange = addressSpace.getEncodedP(
169 1.5.2.2 rmind p, nextCFI, cieInfo->pointerEncoding & 0x0F, ctx);
170 1.5.2.2 rmind // Parse rest of info.
171 1.5.2.2 rmind fdeInfo->lsda = 0;
172 1.5.2.2 rmind // Check for augmentation length
173 1.5.2.2 rmind if (cieInfo->fdesHaveAugmentationData) {
174 1.5.2.2 rmind uintptr_t augLen = addressSpace.getULEB128(p, nextCFI);
175 1.5.2.2 rmind pint_t endOfAug = p + augLen;
176 1.5.2.2 rmind if (cieInfo->lsdaEncoding != DW_EH_PE_omit) {
177 1.5.2.2 rmind // Peek at value (without indirection). Zero means no LSDA.
178 1.5.2.2 rmind pint_t lsdaStart = p;
179 1.5.2.2 rmind if (addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding & 0x0F,
180 1.5.2.2 rmind ctx) != 0) {
181 1.5.2.2 rmind // Reset pointer and re-parse LSDA address.
182 1.5.2.2 rmind p = lsdaStart;
183 1.5.2.2 rmind fdeInfo->lsda =
184 1.5.2.2 rmind addressSpace.getEncodedP(p, nextCFI, cieInfo->lsdaEncoding, ctx);
185 1.5.2.2 rmind }
186 1.5.2.2 rmind }
187 1.5.2.2 rmind p = endOfAug;
188 1.5.2.2 rmind }
189 1.5.2.2 rmind fdeInfo->fdeStart = fdeStart;
190 1.5.2.2 rmind fdeInfo->fdeLength = nextCFI - fdeStart;
191 1.5.2.2 rmind fdeInfo->fdeInstructions = p;
192 1.5.2.2 rmind fdeInfo->pcStart = pcStart;
193 1.5.2.2 rmind fdeInfo->pcEnd = pcStart + pcRange;
194 1.5.2.2 rmind return true;
195 1.5.2.2 rmind }
196 1.5.2.2 rmind
197 1.5.2.2 rmind /// Extract info from a CIE
198 1.5.2.2 rmind template <typename A, typename R>
199 1.5.2.2 rmind bool CFI_Parser<A, R>::parseCIE(A &addressSpace, pint_t cie,
200 1.5.2.2 rmind CIE_Info *cieInfo) {
201 1.5.2.2 rmind cieInfo->pointerEncoding = 0;
202 1.5.2.2 rmind cieInfo->lsdaEncoding = DW_EH_PE_omit;
203 1.5.2.2 rmind cieInfo->personalityEncoding = 0;
204 1.5.2.2 rmind cieInfo->personalityOffsetInCIE = 0;
205 1.5.2.2 rmind cieInfo->personality = 0;
206 1.5.2.2 rmind cieInfo->codeAlignFactor = 0;
207 1.5.2.2 rmind cieInfo->dataAlignFactor = 0;
208 1.5.2.2 rmind cieInfo->isSignalFrame = false;
209 1.5.2.2 rmind cieInfo->fdesHaveAugmentationData = false;
210 1.5.2.2 rmind cieInfo->cieStart = cie;
211 1.5.2.2 rmind pint_t p = cie;
212 1.5.2.2 rmind uint64_t cieLength = addressSpace.get32(p);
213 1.5.2.2 rmind p += 4;
214 1.5.2.2 rmind pint_t cieContentEnd = p + cieLength;
215 1.5.2.2 rmind if (cieLength == 0xffffffff) {
216 1.5.2.2 rmind // 0xffffffff means length is really the next 8 Bytes.
217 1.5.2.2 rmind cieLength = addressSpace.get64(p);
218 1.5.2.2 rmind p += 8;
219 1.5.2.2 rmind cieContentEnd = p + cieLength;
220 1.5.2.2 rmind }
221 1.5.2.2 rmind if (cieLength == 0)
222 1.5.2.2 rmind return true;
223 1.5.2.2 rmind // CIE ID is always 0
224 1.5.2.2 rmind if (addressSpace.get32(p) != 0)
225 1.5.2.2 rmind return false;
226 1.5.2.2 rmind p += 4;
227 1.5.2.2 rmind // Version is always 1 or 3
228 1.5.2.2 rmind uint8_t version = addressSpace.get8(p);
229 1.5.2.2 rmind if (version != 1 && version != 3)
230 1.5.2.2 rmind return false;
231 1.5.2.2 rmind ++p;
232 1.5.2.2 rmind // Save start of augmentation string and find end.
233 1.5.2.2 rmind pint_t strStart = p;
234 1.5.2.2 rmind while (addressSpace.get8(p) != 0)
235 1.5.2.2 rmind ++p;
236 1.5.2.2 rmind ++p;
237 1.5.2.2 rmind // Parse code aligment factor
238 1.5.2.2 rmind cieInfo->codeAlignFactor = addressSpace.getULEB128(p, cieContentEnd);
239 1.5.2.2 rmind // Parse data alignment factor
240 1.5.2.2 rmind cieInfo->dataAlignFactor = addressSpace.getSLEB128(p, cieContentEnd);
241 1.5.2.2 rmind // Parse return address register
242 1.5.2.2 rmind cieInfo->returnAddressRegister = (uint8_t)addressSpace.getULEB128(p, cieContentEnd);
243 1.5.2.2 rmind // Parse augmentation data based on augmentation string.
244 1.5.2.2 rmind if (addressSpace.get8(strStart) == 'z') {
245 1.5.2.2 rmind // parse augmentation data length
246 1.5.2.2 rmind addressSpace.getULEB128(p, cieContentEnd);
247 1.5.2.2 rmind for (pint_t s = strStart; addressSpace.get8(s) != '\0'; ++s) {
248 1.5.2.2 rmind switch (addressSpace.get8(s)) {
249 1.5.2.2 rmind case 'z':
250 1.5.2.2 rmind cieInfo->fdesHaveAugmentationData = true;
251 1.5.2.2 rmind break;
252 1.5.2.2 rmind case 'P':
253 1.5.2.2 rmind cieInfo->personalityEncoding = addressSpace.get8(p);
254 1.5.2.2 rmind ++p;
255 1.5.2.2 rmind cieInfo->personalityOffsetInCIE = p - cie;
256 1.5.2.2 rmind cieInfo->personality = addressSpace.getEncodedP(
257 1.5.2.2 rmind p, cieContentEnd, cieInfo->personalityEncoding, NULL);
258 1.5.2.2 rmind break;
259 1.5.2.2 rmind case 'L':
260 1.5.2.2 rmind cieInfo->lsdaEncoding = addressSpace.get8(p);
261 1.5.2.2 rmind ++p;
262 1.5.2.2 rmind break;
263 1.5.2.2 rmind case 'R':
264 1.5.2.2 rmind cieInfo->pointerEncoding = addressSpace.get8(p);
265 1.5.2.2 rmind ++p;
266 1.5.2.2 rmind break;
267 1.5.2.2 rmind case 'S':
268 1.5.2.2 rmind cieInfo->isSignalFrame = true;
269 1.5.2.2 rmind break;
270 1.5.2.2 rmind default:
271 1.5.2.2 rmind // ignore unknown letters
272 1.5.2.2 rmind break;
273 1.5.2.2 rmind }
274 1.5.2.2 rmind }
275 1.5.2.2 rmind }
276 1.5.2.2 rmind cieInfo->cieLength = cieContentEnd - cieInfo->cieStart;
277 1.5.2.2 rmind cieInfo->cieInstructions = p;
278 1.5.2.2 rmind return true;
279 1.5.2.2 rmind }
280 1.5.2.2 rmind
281 1.5.2.2 rmind /// "Run" the dwarf instructions and create the abstact PrologInfo for an FDE.
282 1.5.2.2 rmind template <typename A, typename R>
283 1.5.2.2 rmind bool CFI_Parser<A, R>::parseFDEInstructions(A &addressSpace,
284 1.5.2.2 rmind const FDE_Info &fdeInfo,
285 1.5.2.2 rmind const CIE_Info &cieInfo,
286 1.5.2.2 rmind pint_t upToPC, PrologInfo *results,
287 1.5.2.2 rmind unw_proc_info_t *ctx) {
288 1.5.2.2 rmind // Clear results.
289 1.5.2.2 rmind memset(results, 0, sizeof(*results));
290 1.5.2.2 rmind PrologInfoStackEntry *rememberStack = NULL;
291 1.5.2.2 rmind
292 1.5.2.2 rmind // First parse the CIE then FDE instructions.
293 1.5.2.2 rmind if (!parseInstructions(addressSpace, cieInfo.cieInstructions,
294 1.5.2.2 rmind cieInfo.cieStart + cieInfo.cieLength, cieInfo,
295 1.5.2.2 rmind (pint_t)(-1), rememberStack, results, ctx))
296 1.5.2.2 rmind return false;
297 1.5.2.2 rmind return parseInstructions(addressSpace, fdeInfo.fdeInstructions,
298 1.5.2.2 rmind fdeInfo.fdeStart + fdeInfo.fdeLength, cieInfo,
299 1.5.2.2 rmind upToPC - fdeInfo.pcStart, rememberStack, results,
300 1.5.2.2 rmind ctx);
301 1.5.2.2 rmind }
302 1.5.2.2 rmind
303 1.5.2.2 rmind /// "Run" the DWARF instructions.
304 1.5.2.2 rmind template <typename A, typename R>
305 1.5.2.2 rmind bool
306 1.5.2.2 rmind CFI_Parser<A, R>::parseInstructions(A &addressSpace, pint_t instructions,
307 1.5.2.2 rmind pint_t instructionsEnd,
308 1.5.2.2 rmind const CIE_Info &cieInfo, pint_t pcoffset,
309 1.5.2.2 rmind PrologInfoStackEntry *&rememberStack,
310 1.5.2.2 rmind PrologInfo *results, unw_proc_info_t *ctx) {
311 1.5.2.2 rmind pint_t p = instructions;
312 1.5.2.2 rmind uint32_t codeOffset = 0;
313 1.5.2.2 rmind PrologInfo initialState = *results;
314 1.5.2.2 rmind
315 1.5.2.2 rmind // See Dwarf Spec, section 6.4.2 for details on unwind opcodes.
316 1.5.2.2 rmind while (p < instructionsEnd && codeOffset < pcoffset) {
317 1.5.2.2 rmind uint64_t reg;
318 1.5.2.2 rmind uint64_t reg2;
319 1.5.2.2 rmind int64_t offset;
320 1.5.2.2 rmind uint64_t length;
321 1.5.2.2 rmind uint8_t opcode = addressSpace.get8(p);
322 1.5.2.2 rmind uint8_t operand;
323 1.5.2.2 rmind PrologInfoStackEntry *entry;
324 1.5.2.2 rmind ++p;
325 1.5.2.2 rmind switch (opcode) {
326 1.5.2.2 rmind case DW_CFA_nop:
327 1.5.2.2 rmind break;
328 1.5.2.2 rmind case DW_CFA_set_loc:
329 1.5.2.2 rmind codeOffset = addressSpace.getEncodedP(p, instructionsEnd,
330 1.5.2.2 rmind cieInfo.pointerEncoding, ctx);
331 1.5.2.2 rmind break;
332 1.5.2.2 rmind case DW_CFA_advance_loc1:
333 1.5.2.2 rmind codeOffset += (addressSpace.get8(p) * cieInfo.codeAlignFactor);
334 1.5.2.2 rmind p += 1;
335 1.5.2.2 rmind break;
336 1.5.2.2 rmind case DW_CFA_advance_loc2:
337 1.5.2.2 rmind codeOffset += (addressSpace.get16(p) * cieInfo.codeAlignFactor);
338 1.5.2.2 rmind p += 2;
339 1.5.2.2 rmind break;
340 1.5.2.2 rmind case DW_CFA_advance_loc4:
341 1.5.2.2 rmind codeOffset += (addressSpace.get32(p) * cieInfo.codeAlignFactor);
342 1.5.2.2 rmind p += 4;
343 1.5.2.2 rmind break;
344 1.5.2.2 rmind case DW_CFA_offset_extended:
345 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
346 1.5.2.2 rmind offset =
347 1.5.2.2 rmind addressSpace.getULEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
348 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
349 1.5.2.2 rmind return false;
350 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInCFA;
351 1.5.2.2 rmind results->savedRegisters[reg].value = offset;
352 1.5.2.2 rmind break;
353 1.5.2.2 rmind case DW_CFA_restore_extended:
354 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
355 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
356 1.5.2.2 rmind return false;
357 1.5.2.2 rmind results->savedRegisters[reg] = initialState.savedRegisters[reg];
358 1.5.2.2 rmind break;
359 1.5.2.2 rmind case DW_CFA_undefined:
360 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
361 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
362 1.5.2.2 rmind return false;
363 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterUnused;
364 1.5.2.2 rmind break;
365 1.5.2.2 rmind case DW_CFA_same_value:
366 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
367 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
368 1.5.2.2 rmind return false;
369 1.5.2.2 rmind // "same value" means register was stored in frame, but its current
370 1.5.2.2 rmind // value has not changed, so no need to restore from frame.
371 1.5.2.2 rmind // We model this as if the register was never saved.
372 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterUnused;
373 1.5.2.2 rmind break;
374 1.5.2.2 rmind case DW_CFA_register:
375 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
376 1.5.2.2 rmind reg2 = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
377 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
378 1.5.2.2 rmind return false;
379 1.5.2.2 rmind if (reg2 > kMaxRegisterNumber)
380 1.5.2.2 rmind return false;
381 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInRegister;
382 1.5.2.2 rmind results->savedRegisters[reg].value = reg2;
383 1.5.2.2 rmind break;
384 1.5.2.2 rmind case DW_CFA_remember_state:
385 1.5.2.2 rmind // avoid operator new, because that would be an upward dependency
386 1.5.2.2 rmind entry = (PrologInfoStackEntry *)malloc(sizeof(PrologInfoStackEntry));
387 1.5.2.2 rmind if (entry == NULL)
388 1.5.2.2 rmind return false;
389 1.5.2.2 rmind
390 1.5.2.2 rmind entry->next = rememberStack;
391 1.5.2.2 rmind entry->info = *results;
392 1.5.2.2 rmind rememberStack = entry;
393 1.5.2.2 rmind break;
394 1.5.2.2 rmind case DW_CFA_restore_state:
395 1.5.2.2 rmind if (rememberStack == NULL)
396 1.5.2.2 rmind return false;
397 1.5.2.2 rmind {
398 1.5.2.2 rmind PrologInfoStackEntry *top = rememberStack;
399 1.5.2.2 rmind *results = top->info;
400 1.5.2.2 rmind rememberStack = top->next;
401 1.5.2.2 rmind free((char *)top);
402 1.5.2.2 rmind }
403 1.5.2.2 rmind break;
404 1.5.2.2 rmind case DW_CFA_def_cfa:
405 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
406 1.5.2.2 rmind offset = addressSpace.getULEB128(p, instructionsEnd);
407 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
408 1.5.2.2 rmind return false;
409 1.5.2.2 rmind results->cfaRegister = reg;
410 1.5.2.2 rmind results->cfaRegisterOffset = offset;
411 1.5.2.2 rmind break;
412 1.5.2.2 rmind case DW_CFA_def_cfa_register:
413 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
414 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
415 1.5.2.2 rmind return false;
416 1.5.2.2 rmind results->cfaRegister = reg;
417 1.5.2.2 rmind break;
418 1.5.2.2 rmind case DW_CFA_def_cfa_offset:
419 1.5.2.2 rmind results->cfaRegisterOffset = addressSpace.getULEB128(p, instructionsEnd);
420 1.5.2.2 rmind results->codeOffsetAtStackDecrement = codeOffset;
421 1.5.2.2 rmind break;
422 1.5.2.2 rmind case DW_CFA_def_cfa_expression:
423 1.5.2.2 rmind results->cfaRegister = 0;
424 1.5.2.2 rmind results->cfaExpression = p;
425 1.5.2.2 rmind length = addressSpace.getULEB128(p, instructionsEnd);
426 1.5.2.2 rmind p += length;
427 1.5.2.2 rmind break;
428 1.5.2.2 rmind case DW_CFA_expression:
429 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
430 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
431 1.5.2.2 rmind return false;
432 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterAtExpression;
433 1.5.2.2 rmind results->savedRegisters[reg].value = p;
434 1.5.2.2 rmind length = addressSpace.getULEB128(p, instructionsEnd);
435 1.5.2.2 rmind p += length;
436 1.5.2.2 rmind break;
437 1.5.2.2 rmind case DW_CFA_offset_extended_sf:
438 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
439 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
440 1.5.2.2 rmind return false;
441 1.5.2.2 rmind offset =
442 1.5.2.2 rmind addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
443 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInCFA;
444 1.5.2.2 rmind results->savedRegisters[reg].value = offset;
445 1.5.2.2 rmind break;
446 1.5.2.2 rmind case DW_CFA_def_cfa_sf:
447 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
448 1.5.2.2 rmind offset =
449 1.5.2.2 rmind addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
450 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
451 1.5.2.2 rmind return false;
452 1.5.2.2 rmind results->cfaRegister = reg;
453 1.5.2.2 rmind results->cfaRegisterOffset = offset;
454 1.5.2.2 rmind break;
455 1.5.2.2 rmind case DW_CFA_def_cfa_offset_sf:
456 1.5.2.2 rmind results->cfaRegisterOffset =
457 1.5.2.2 rmind addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
458 1.5.2.2 rmind results->codeOffsetAtStackDecrement = codeOffset;
459 1.5.2.2 rmind break;
460 1.5.2.2 rmind case DW_CFA_val_offset:
461 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
462 1.5.2.2 rmind offset =
463 1.5.2.2 rmind addressSpace.getULEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
464 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
465 1.5.2.2 rmind return false;
466 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterOffsetFromCFA;
467 1.5.2.2 rmind results->savedRegisters[reg].value = offset;
468 1.5.2.2 rmind break;
469 1.5.2.2 rmind case DW_CFA_val_offset_sf:
470 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
471 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
472 1.5.2.2 rmind return false;
473 1.5.2.2 rmind offset =
474 1.5.2.2 rmind addressSpace.getSLEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
475 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterOffsetFromCFA;
476 1.5.2.2 rmind results->savedRegisters[reg].value = offset;
477 1.5.2.2 rmind break;
478 1.5.2.2 rmind case DW_CFA_val_expression:
479 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
480 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
481 1.5.2.2 rmind return false;
482 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterIsExpression;
483 1.5.2.2 rmind results->savedRegisters[reg].value = p;
484 1.5.2.2 rmind length = addressSpace.getULEB128(p, instructionsEnd);
485 1.5.2.2 rmind p += length;
486 1.5.2.2 rmind break;
487 1.5.2.2 rmind case DW_CFA_GNU_window_save:
488 1.5.2.2 rmind #if defined(__sparc__)
489 1.5.2.2 rmind for (reg = 8; reg < 16; ++reg) {
490 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInRegister;
491 1.5.2.2 rmind results->savedRegisters[reg].value = reg + 16;
492 1.5.2.2 rmind }
493 1.5.2.2 rmind for (reg = 16; reg < 32; ++reg) {
494 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInCFA;
495 1.5.2.2 rmind results->savedRegisters[reg].value = (reg - 16) * sizeof(typename R::reg_t);
496 1.5.2.2 rmind }
497 1.5.2.2 rmind break;
498 1.5.2.2 rmind #else
499 1.5.2.2 rmind return false;
500 1.5.2.2 rmind #endif
501 1.5.2.2 rmind case DW_CFA_GNU_args_size:
502 1.5.2.2 rmind offset = addressSpace.getULEB128(p, instructionsEnd);
503 1.5.2.2 rmind results->spExtraArgSize = offset;
504 1.5.2.2 rmind break;
505 1.5.2.2 rmind case DW_CFA_GNU_negative_offset_extended:
506 1.5.2.2 rmind reg = R::dwarf2regno(addressSpace.getULEB128(p, instructionsEnd));
507 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
508 1.5.2.2 rmind return false;
509 1.5.2.2 rmind offset =
510 1.5.2.2 rmind addressSpace.getULEB128(p, instructionsEnd) * cieInfo.dataAlignFactor;
511 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInCFA;
512 1.5.2.2 rmind results->savedRegisters[reg].value = -offset;
513 1.5.2.2 rmind break;
514 1.5.2.2 rmind default:
515 1.5.2.2 rmind operand = opcode & 0x3F;
516 1.5.2.2 rmind switch (opcode & 0xC0) {
517 1.5.2.2 rmind case DW_CFA_offset:
518 1.5.2.2 rmind reg = R::dwarf2regno(operand);
519 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
520 1.5.2.2 rmind return false;
521 1.5.2.2 rmind offset = addressSpace.getULEB128(p, instructionsEnd) *
522 1.5.2.2 rmind cieInfo.dataAlignFactor;
523 1.5.2.2 rmind results->savedRegisters[reg].location = kRegisterInCFA;
524 1.5.2.2 rmind results->savedRegisters[reg].value = offset;
525 1.5.2.2 rmind break;
526 1.5.2.2 rmind case DW_CFA_advance_loc:
527 1.5.2.2 rmind codeOffset += operand * cieInfo.codeAlignFactor;
528 1.5.2.2 rmind break;
529 1.5.2.2 rmind case DW_CFA_restore:
530 1.5.2.2 rmind reg = R::dwarf2regno(operand);
531 1.5.2.2 rmind if (reg > kMaxRegisterNumber)
532 1.5.2.2 rmind return false;
533 1.5.2.2 rmind results->savedRegisters[reg] = initialState.savedRegisters[reg];
534 1.5.2.2 rmind break;
535 1.5.2.2 rmind default:
536 1.5.2.2 rmind return false;
537 1.5.2.2 rmind }
538 1.5.2.2 rmind }
539 1.5.2.2 rmind }
540 1.5.2.2 rmind
541 1.5.2.2 rmind return true;
542 1.5.2.2 rmind }
543 1.5.2.2 rmind
544 1.5.2.2 rmind } // namespace _Unwind
545 1.5.2.2 rmind
546 1.5.2.2 rmind #endif // __DWARF_PARSER_HPP__
547