1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
26 // processor specific parsing of dwarf unwind instructions
29 #ifndef __DWARF_INSTRUCTIONS_HPP__
30 #define __DWARF_INSTRUCTIONS_HPP__
39 #include <libunwind.h>
40 #include <mach-o/compact_unwind_encoding.h>
43 #include "AddressSpace.hpp"
44 #include "Registers.hpp"
45 #include "DwarfParser.hpp"
46 #include "InternalMacros.h"
47 //#include "CompactUnwinder.hpp"
49 #define EXTRACT_BITS(value, mask) \
50 ( (value >> __builtin_ctz(mask)) & (((1 << __builtin_popcount(mask)))-1) )
52 #define CFI_INVALID_ADDRESS ((pint_t)(-1))
57 /// Used by linker when parsing __eh_frame section
60 struct CFI_Reference {
61 typedef typename A::pint_t pint_t;
62 uint8_t encodingOfTargetAddress;
67 struct CFI_Atom_Info {
68 typedef typename A::pint_t pint_t;
74 CFI_Reference<A> function;
76 CFI_Reference<A> lsda;
77 uint32_t compactUnwindInfo;
80 CFI_Reference<A> personality;
85 typedef void (*WarnFunc)(void* ref, uint64_t funcAddr, const char* msg);
88 /// DwarfInstructions maps abtract dwarf unwind instructions to a particular architecture
90 template <typename A, typename R>
91 class DwarfInstructions
94 typedef typename A::pint_t pint_t;
95 typedef typename A::sint_t sint_t;
97 static const char* parseCFIs(A& addressSpace, pint_t ehSectionStart, uint32_t sectionLength,
98 const pint_t cuStarts[], uint32_t cuCount, bool keepDwarfWhichHasCU, bool forceDwarfConversion,
99 CFI_Atom_Info<A>* infos, uint32_t& infosCount, void* ref, WarnFunc warn);
102 static compact_unwind_encoding_t createCompactEncodingFromFDE(A& addressSpace, pint_t fdeStart,
103 pint_t* lsda, pint_t* personality,
104 char warningBuffer[1024]);
106 static int stepWithDwarf(A& addressSpace, pint_t pc, pint_t fdeStart, R& registers);
111 DW_X86_64_RET_ADDR = 16
118 static pint_t evaluateExpression(pint_t expression, A& addressSpace, const R& registers, pint_t initialStackValue);
119 static pint_t getSavedRegister(A& addressSpace, const R& registers, pint_t cfa,
120 const typename CFI_Parser<A>::RegisterLocation& savedReg);
121 static double getSavedFloatRegister(A& addressSpace, const R& registers, pint_t cfa,
122 const typename CFI_Parser<A>::RegisterLocation& savedReg);
123 static v128 getSavedVectorRegister(A& addressSpace, const R& registers, pint_t cfa,
124 const typename CFI_Parser<A>::RegisterLocation& savedReg);
126 // x86 specific variants
127 static int lastRestoreReg(const Registers_x86&);
128 static bool isReturnAddressRegister(int regNum, const Registers_x86&);
129 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_x86&);
131 static uint32_t getEBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure);
132 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_x86&);
133 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
134 const Registers_x86&, const typename CFI_Parser<A>::PrologInfo& prolog,
135 char warningBuffer[1024]);
137 // x86_64 specific variants
138 static int lastRestoreReg(const Registers_x86_64&);
139 static bool isReturnAddressRegister(int regNum, const Registers_x86_64&);
140 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_x86_64&);
142 static uint32_t getRBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure);
143 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_x86_64&);
144 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
145 const Registers_x86_64&, const typename CFI_Parser<A>::PrologInfo& prolog,
146 char warningBuffer[1024]);
148 // ppc specific variants
149 static int lastRestoreReg(const Registers_ppc&);
150 static bool isReturnAddressRegister(int regNum, const Registers_ppc&);
151 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_ppc&);
152 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_ppc&);
153 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
154 const Registers_ppc&, const typename CFI_Parser<A>::PrologInfo& prolog,
155 char warningBuffer[1024]);
157 // arm64 specific variants
158 static bool isReturnAddressRegister(int regNum, const Registers_arm64&);
159 static int lastRestoreReg(const Registers_arm64&);
160 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_arm64&);
161 static bool checkRegisterPair(uint32_t reg, const typename CFI_Parser<A>::PrologInfo& prolog,
162 int& offset, char warningBuffer[1024]);
163 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_arm64&);
164 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
165 const Registers_arm64&, const typename CFI_Parser<A>::PrologInfo& prolog,
166 char warningBuffer[1024]);
173 template <typename A, typename R>
174 const char* DwarfInstructions<A,R>::parseCFIs(A& addressSpace, pint_t ehSectionStart, uint32_t sectionLength,
175 const pint_t cuStarts[], uint32_t cuCount, bool keepDwarfWhichHasCU, bool forceDwarfConversion,
176 CFI_Atom_Info<A>* infos, uint32_t& infosCount, void* ref, WarnFunc warn)
178 typename CFI_Parser<A>::CIE_Info cieInfo;
179 CFI_Atom_Info<A>* entry = infos;
180 CFI_Atom_Info<A>* end = &infos[infosCount];
181 const pint_t ehSectionEnd = ehSectionStart + sectionLength;
182 for (pint_t p=ehSectionStart; p < ehSectionEnd; ) {
183 pint_t currentCFI = p;
184 uint64_t cfiLength = addressSpace.get32(p);
186 if ( cfiLength == 0xffffffff ) {
187 // 0xffffffff means length is really next 8 bytes
188 cfiLength = addressSpace.get64(p);
191 if ( cfiLength == 0 )
192 return NULL; // end marker
194 return "too little space allocated for parseCFIs";
195 pint_t nextCFI = p + cfiLength;
196 uint32_t id = addressSpace.get32(p);
199 const char* err = CFI_Parser<A>::parseCIE(addressSpace, currentCFI, &cieInfo);
202 entry->address = currentCFI;
203 entry->size = nextCFI - currentCFI;
205 entry->u.cieInfo.personality.targetAddress = cieInfo.personality;
206 entry->u.cieInfo.personality.offsetInCFI = cieInfo.personalityOffsetInCIE;
207 entry->u.cieInfo.personality.encodingOfTargetAddress = cieInfo.personalityEncoding;
212 entry->address = currentCFI;
213 entry->size = nextCFI - currentCFI;
214 entry->isCIE = false;
215 entry->u.fdeInfo.function.targetAddress = CFI_INVALID_ADDRESS;
216 entry->u.fdeInfo.cie.targetAddress = CFI_INVALID_ADDRESS;
217 entry->u.fdeInfo.lsda.targetAddress = CFI_INVALID_ADDRESS;
218 uint32_t ciePointer = addressSpace.get32(p);
219 pint_t cieStart = p-ciePointer;
220 // validate pointer to CIE is within section
221 if ( (cieStart < ehSectionStart) || (cieStart > ehSectionEnd) )
222 return "FDE points to CIE outside __eh_frame section";
223 // optimize usual case where cie is same for all FDEs
224 if ( cieStart != cieInfo.cieStart ) {
225 const char* err = CFI_Parser<A>::parseCIE(addressSpace, cieStart, &cieInfo);
229 entry->u.fdeInfo.cie.targetAddress = cieStart;
230 entry->u.fdeInfo.cie.offsetInCFI = p-currentCFI;
231 entry->u.fdeInfo.cie.encodingOfTargetAddress = DW_EH_PE_sdata4 | DW_EH_PE_pcrel;
233 // parse pc begin and range
234 pint_t offsetOfFunctionAddress = p-currentCFI;
235 pint_t pcStart = addressSpace.getEncodedP(p, nextCFI, cieInfo.pointerEncoding);
236 pint_t pcRange = addressSpace.getEncodedP(p, nextCFI, cieInfo.pointerEncoding & 0x0F);
237 //fprintf(stderr, "FDE with pcRange [0x%08llX, 0x%08llX)\n",(uint64_t)pcStart, (uint64_t)(pcStart+pcRange));
238 entry->u.fdeInfo.function.targetAddress = pcStart;
239 entry->u.fdeInfo.function.offsetInCFI = offsetOfFunctionAddress;
240 entry->u.fdeInfo.function.encodingOfTargetAddress = cieInfo.pointerEncoding;
241 // check for augmentation length
242 if ( cieInfo.fdesHaveAugmentationData ) {
243 uintptr_t augLen = addressSpace.getULEB128(p, nextCFI);
244 pint_t endOfAug = p + augLen;
245 if ( cieInfo.lsdaEncoding != 0 ) {
246 // peek at value (without indirection). Zero means no lsda
247 pint_t lsdaStart = p;
248 if ( addressSpace.getEncodedP(p, nextCFI, cieInfo.lsdaEncoding & 0x0F) != 0 ) {
249 // reset pointer and re-parse lsda address
251 pint_t offsetOfLSDAAddress = p-currentCFI;
252 entry->u.fdeInfo.lsda.targetAddress = addressSpace.getEncodedP(p, nextCFI, cieInfo.lsdaEncoding);
253 entry->u.fdeInfo.lsda.offsetInCFI = offsetOfLSDAAddress;
254 entry->u.fdeInfo.lsda.encodingOfTargetAddress = cieInfo.lsdaEncoding;
259 // See if already is a compact unwind for this address.
260 bool alreadyHaveCU = false;
261 for (uint32_t i=0; i < cuCount; ++i) {
262 if (cuStarts[i] == entry->u.fdeInfo.function.targetAddress) {
263 alreadyHaveCU = true;
267 //fprintf(stderr, "FDE for func at 0x%08X, alreadyHaveCU=%d\n", (uint32_t)entry->u.fdeInfo.function.targetAddress, alreadyHaveCU);
268 if ( alreadyHaveCU && !forceDwarfConversion ) {
269 if ( keepDwarfWhichHasCU )
273 if ( (cuCount != 0) && !forceDwarfConversion ) {
274 // Have some compact unwind, so this is a new .o file, therefore anything without
275 // compact unwind must be something not expressable in compact unwind.
277 entry->u.fdeInfo.compactUnwindInfo = encodeToUseDwarf(dummy);
280 // compute compact unwind encoding by parsing dwarf
281 typename CFI_Parser<A>::FDE_Info fdeInfo;
282 fdeInfo.fdeStart = currentCFI;
283 fdeInfo.fdeLength = nextCFI - currentCFI;
284 fdeInfo.fdeInstructions = p;
285 fdeInfo.pcStart = pcStart;
286 fdeInfo.pcEnd = pcStart + pcRange;
287 fdeInfo.lsda = entry->u.fdeInfo.lsda.targetAddress;
288 typename CFI_Parser<A>::PrologInfo prolog;
289 R dummy; // for proper selection of architecture specific functions
290 if ( CFI_Parser<A>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo, CFI_INVALID_ADDRESS, &prolog) ) {
291 char warningBuffer[1024];
292 entry->u.fdeInfo.compactUnwindInfo = createCompactEncodingFromProlog(addressSpace, fdeInfo.pcStart, dummy, prolog, warningBuffer);
293 if ( fdeInfo.lsda != CFI_INVALID_ADDRESS )
294 entry->u.fdeInfo.compactUnwindInfo |= UNWIND_HAS_LSDA;
295 if ( warningBuffer[0] != '\0' )
296 warn(ref, fdeInfo.pcStart, warningBuffer);
299 warn(ref, CFI_INVALID_ADDRESS, "dwarf unwind instructions could not be parsed");
300 entry->u.fdeInfo.compactUnwindInfo = encodeToUseDwarf(dummy);
308 if ( entry != end ) {
309 //fprintf(stderr, "DwarfInstructions<A,R>::parseCFIs() infosCount was %d on input, now %ld\n", infosCount, entry - infos);
310 infosCount = (entry - infos);
313 return NULL; // success
319 template <typename A, typename R>
320 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromFDE(A& addressSpace, pint_t fdeStart,
321 pint_t* lsda, pint_t* personality,
322 char warningBuffer[1024])
324 typename CFI_Parser<A>::FDE_Info fdeInfo;
325 typename CFI_Parser<A>::CIE_Info cieInfo;
326 R dummy; // for proper selection of architecture specific functions
327 if ( CFI_Parser<A>::decodeFDE(addressSpace, fdeStart, &fdeInfo, &cieInfo) == NULL ) {
328 typename CFI_Parser<A>::PrologInfo prolog;
329 if ( CFI_Parser<A>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo, CFI_INVALID_ADDRESS, &prolog) ) {
330 *lsda = fdeInfo.lsda;
331 *personality = cieInfo.personality;
332 compact_unwind_encoding_t encoding;
333 encoding = createCompactEncodingFromProlog(addressSpace, fdeInfo.pcStart, dummy, prolog, warningBuffer);
334 if ( fdeInfo.lsda != 0 )
335 encoding |= UNWIND_HAS_LSDA;
339 strcpy(warningBuffer, "dwarf unwind instructions could not be parsed");
340 return encodeToUseDwarf(dummy);
344 strcpy(warningBuffer, "dwarf FDE could not be parsed");
345 return encodeToUseDwarf(dummy);
350 template <typename A, typename R>
351 typename A::pint_t DwarfInstructions<A,R>::getSavedRegister(A& addressSpace, const R& registers, pint_t cfa,
352 const typename CFI_Parser<A>::RegisterLocation& savedReg)
354 switch ( savedReg.location ) {
355 case CFI_Parser<A>::kRegisterInCFA:
356 return addressSpace.getP(cfa + savedReg.value);
358 case CFI_Parser<A>::kRegisterAtExpression:
359 return addressSpace.getP(evaluateExpression(savedReg.value, addressSpace, registers, cfa));
361 case CFI_Parser<A>::kRegisterIsExpression:
362 return evaluateExpression(savedReg.value, addressSpace, registers, cfa);
364 case CFI_Parser<A>::kRegisterInRegister:
365 return registers.getRegister(savedReg.value);
367 case CFI_Parser<A>::kRegisterUnused:
368 case CFI_Parser<A>::kRegisterOffsetFromCFA:
372 ABORT("unsupported restore location for register");
375 template <typename A, typename R>
376 double DwarfInstructions<A,R>::getSavedFloatRegister(A& addressSpace, const R& registers, pint_t cfa,
377 const typename CFI_Parser<A>::RegisterLocation& savedReg)
379 switch ( savedReg.location ) {
380 case CFI_Parser<A>::kRegisterInCFA:
381 return addressSpace.getDouble(cfa + savedReg.value);
383 case CFI_Parser<A>::kRegisterAtExpression:
384 return addressSpace.getDouble(evaluateExpression(savedReg.value, addressSpace, registers, cfa));
386 case CFI_Parser<A>::kRegisterIsExpression:
387 case CFI_Parser<A>::kRegisterUnused:
388 case CFI_Parser<A>::kRegisterOffsetFromCFA:
389 case CFI_Parser<A>::kRegisterInRegister:
393 ABORT("unsupported restore location for float register");
396 template <typename A, typename R>
397 v128 DwarfInstructions<A,R>::getSavedVectorRegister(A& addressSpace, const R& registers, pint_t cfa,
398 const typename CFI_Parser<A>::RegisterLocation& savedReg)
400 switch ( savedReg.location ) {
401 case CFI_Parser<A>::kRegisterInCFA:
402 return addressSpace.getVector(cfa + savedReg.value);
404 case CFI_Parser<A>::kRegisterAtExpression:
405 return addressSpace.getVector(evaluateExpression(savedReg.value, addressSpace, registers, cfa));
407 case CFI_Parser<A>::kRegisterIsExpression:
408 case CFI_Parser<A>::kRegisterUnused:
409 case CFI_Parser<A>::kRegisterOffsetFromCFA:
410 case CFI_Parser<A>::kRegisterInRegister:
414 ABORT("unsupported restore location for vector register");
418 template <typename A, typename R>
419 int DwarfInstructions<A,R>::stepWithDwarf(A& addressSpace, pint_t pc, pint_t fdeStart, R& registers)
421 //fprintf(stderr, "stepWithDwarf(pc=0x%0llX, fdeStart=0x%0llX)\n", (uint64_t)pc, (uint64_t)fdeStart);
422 typename CFI_Parser<A>::FDE_Info fdeInfo;
423 typename CFI_Parser<A>::CIE_Info cieInfo;
424 if ( CFI_Parser<A>::decodeFDE(addressSpace, fdeStart, &fdeInfo, &cieInfo) == NULL ) {
425 typename CFI_Parser<A>::PrologInfo prolog;
426 if ( CFI_Parser<A>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo, pc, &prolog) ) {
427 R newRegisters = registers;
429 // get pointer to cfa (architecture specific)
430 pint_t cfa = getCFA(addressSpace, prolog, registers);
432 // restore registers that dwarf says were saved
433 pint_t returnAddress = 0;
434 for (int i=0; i <= lastRestoreReg(newRegisters); ++i) {
435 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
436 if ( registers.validFloatRegister(i) )
437 newRegisters.setFloatRegister(i, getSavedFloatRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]));
438 else if ( registers.validVectorRegister(i) )
439 newRegisters.setVectorRegister(i, getSavedVectorRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]));
440 else if ( isReturnAddressRegister(i, registers) )
441 returnAddress = getSavedRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]);
442 else if ( registers.validRegister(i) )
443 newRegisters.setRegister(i, getSavedRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]));
449 // by definition the CFA is the stack pointer at the call site, so restoring SP means setting it to CFA
450 newRegisters.setSP(cfa);
452 // return address is address after call site instruction, so setting IP to that does a return
453 newRegisters.setIP(returnAddress);
455 // do the actual step by replacing the register set with the new ones
456 registers = newRegisters;
458 return UNW_STEP_SUCCESS;
461 return UNW_EBADFRAME;
466 template <typename A, typename R>
467 typename A::pint_t DwarfInstructions<A,R>::evaluateExpression(pint_t expression, A& addressSpace,
468 const R& registers, pint_t initialStackValue)
470 const bool log = false;
471 pint_t p = expression;
472 pint_t expressionEnd = expression+20; // just need something until length is read
473 uint64_t length = addressSpace.getULEB128(p, expressionEnd);
474 expressionEnd = p + length;
475 if (log) fprintf(stderr, "evaluateExpression(): length=%llu\n", length);
478 *(++sp) = initialStackValue;
480 while ( p < expressionEnd ) {
482 for(pint_t* t = sp; t > stack; --t) {
483 fprintf(stderr, "sp[] = 0x%llX\n", (uint64_t)(*t));
486 uint8_t opcode = addressSpace.get8(p++);
492 // push immediate address sized value
493 value = addressSpace.getP(p);
496 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
500 // pop stack, dereference, push result
502 *(++sp) = addressSpace.getP(value);
503 if (log) fprintf(stderr, "dereference 0x%llX\n", (uint64_t)value);
507 // push immediate 1 byte value
508 value = addressSpace.get8(p);
511 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
515 // push immediate 1 byte signed value
516 svalue = (int8_t)addressSpace.get8(p);
519 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
523 // push immediate 2 byte value
524 value = addressSpace.get16(p);
527 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
531 // push immediate 2 byte signed value
532 svalue = (int16_t)addressSpace.get16(p);
535 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
539 // push immediate 4 byte value
540 value = addressSpace.get32(p);
543 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
547 // push immediate 4 byte signed value
548 svalue = (int32_t)addressSpace.get32(p);
551 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
555 // push immediate 8 byte value
556 value = addressSpace.get64(p);
559 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
563 // push immediate 8 byte signed value
564 value = (int32_t)addressSpace.get64(p);
567 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
571 // push immediate ULEB128 value
572 value = addressSpace.getULEB128(p, expressionEnd);
574 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
578 // push immediate SLEB128 value
579 svalue = addressSpace.getSLEB128(p, expressionEnd);
581 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
588 if (log) fprintf(stderr, "duplicate top of stack\n");
594 if (log) fprintf(stderr, "pop top of stack\n");
601 if (log) fprintf(stderr, "duplicate second in stack\n");
606 reg = addressSpace.get8(p);
610 if (log) fprintf(stderr, "duplicate %d in stack\n", reg);
618 if (log) fprintf(stderr, "swap top of stack\n");
627 if (log) fprintf(stderr, "rotate top three of stack\n");
631 // pop stack, dereference, push result
633 *sp = *((uint64_t*)value);
634 if (log) fprintf(stderr, "x-dereference 0x%llX\n", (uint64_t)value);
641 if (log) fprintf(stderr, "abs\n");
647 if (log) fprintf(stderr, "and\n");
653 if (log) fprintf(stderr, "div\n");
659 if (log) fprintf(stderr, "minus\n");
665 if (log) fprintf(stderr, "module\n");
671 if (log) fprintf(stderr, "mul\n");
676 if (log) fprintf(stderr, "neg\n");
682 if (log) fprintf(stderr, "not\n");
688 if (log) fprintf(stderr, "or\n");
694 if (log) fprintf(stderr, "plus\n");
697 case DW_OP_plus_uconst:
698 // pop stack, add uelb128 constant, push result
699 *sp += addressSpace.getULEB128(p, expressionEnd);
700 if (log) fprintf(stderr, "add constant\n");
706 if (log) fprintf(stderr, "shift left\n");
712 if (log) fprintf(stderr, "shift left\n");
718 *sp = svalue >> value;
719 if (log) fprintf(stderr, "shift left arithmetric\n");
725 if (log) fprintf(stderr, "xor\n");
729 svalue = (int16_t)addressSpace.get16(p);
732 if (log) fprintf(stderr, "skip %lld\n", (uint64_t)svalue);
736 svalue = (int16_t)addressSpace.get16(p);
740 if (log) fprintf(stderr, "bra %lld\n", (uint64_t)svalue);
745 *sp = (*sp == value);
746 if (log) fprintf(stderr, "eq\n");
751 *sp = (*sp >= value);
752 if (log) fprintf(stderr, "ge\n");
758 if (log) fprintf(stderr, "gt\n");
763 *sp = (*sp <= value);
764 if (log) fprintf(stderr, "le\n");
770 if (log) fprintf(stderr, "lt\n");
775 *sp = (*sp != value);
776 if (log) fprintf(stderr, "ne\n");
811 value = opcode - DW_OP_lit0;
813 if (log) fprintf(stderr, "push literal 0x%llX\n", (uint64_t)value);
848 reg = opcode - DW_OP_reg0;
849 *(++sp) = registers.getRegister(reg);
850 if (log) fprintf(stderr, "push reg %d\n", reg);
854 reg = addressSpace.getULEB128(p, expressionEnd);
855 *(++sp) = registers.getRegister(reg);
856 if (log) fprintf(stderr, "push reg %d + 0x%llX\n", reg, (uint64_t)svalue);
891 reg = opcode - DW_OP_breg0;
892 svalue = addressSpace.getSLEB128(p, expressionEnd);
893 *(++sp) = registers.getRegister(reg) + svalue;
894 if (log) fprintf(stderr, "push reg %d + 0x%llX\n", reg, (uint64_t)svalue);
898 reg = addressSpace.getULEB128(p, expressionEnd);
899 svalue = addressSpace.getSLEB128(p, expressionEnd);
900 *(++sp) = registers.getRegister(reg) + svalue;
901 if (log) fprintf(stderr, "push reg %d + 0x%llX\n", reg, (uint64_t)svalue);
905 ABORT("DW_OP_fbreg not implemented");
909 ABORT("DW_OP_piece not implemented");
912 case DW_OP_deref_size:
913 // pop stack, dereference, push result
915 switch ( addressSpace.get8(p++) ) {
917 value = addressSpace.get8(value);
920 value = addressSpace.get16(value);
923 value = addressSpace.get32(value);
926 value = addressSpace.get64(value);
929 ABORT("DW_OP_deref_size with bad size");
932 if (log) fprintf(stderr, "sized dereference 0x%llX\n", (uint64_t)value);
935 case DW_OP_xderef_size:
937 case DW_OP_push_object_addres:
942 ABORT("dwarf opcode not implemented");
946 if (log) fprintf(stderr, "expression evaluates to 0x%llX\n", (uint64_t)*sp);
953 // x86_64 specific functions
956 template <typename A, typename R>
957 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_x86_64&)
959 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)DW_X86_64_RET_ADDR );
960 return DW_X86_64_RET_ADDR;
963 template <typename A, typename R>
964 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_x86_64&)
966 return (regNum == DW_X86_64_RET_ADDR);
969 template <typename A, typename R>
970 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
971 const Registers_x86_64& registers)
973 if ( prolog.cfaRegister != 0 )
974 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
975 else if ( prolog.cfaExpression != 0 )
976 return evaluateExpression(prolog.cfaExpression, addressSpace, registers, 0);
978 ABORT("getCFA(): unknown location for x86_64 cfa");
983 template <typename A, typename R>
984 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_x86_64&)
986 return UNWIND_X86_64_MODE_DWARF;
989 template <typename A, typename R>
990 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_x86&)
992 return UNWIND_X86_MODE_DWARF;
997 template <typename A, typename R>
998 uint32_t DwarfInstructions<A,R>::getRBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure)
1000 if ( (regOffsetFromBaseOffset < 0) || (regOffsetFromBaseOffset > 32) ) {
1004 unsigned int slotIndex = regOffsetFromBaseOffset/8;
1007 case UNW_X86_64_RBX:
1008 return UNWIND_X86_64_REG_RBX << (slotIndex*3);
1009 case UNW_X86_64_R12:
1010 return UNWIND_X86_64_REG_R12 << (slotIndex*3);
1011 case UNW_X86_64_R13:
1012 return UNWIND_X86_64_REG_R13 << (slotIndex*3);
1013 case UNW_X86_64_R14:
1014 return UNWIND_X86_64_REG_R14 << (slotIndex*3);
1015 case UNW_X86_64_R15:
1016 return UNWIND_X86_64_REG_R15 << (slotIndex*3);
1026 template <typename A, typename R>
1027 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
1028 const Registers_x86_64& r, const typename CFI_Parser<A>::PrologInfo& prolog,
1029 char warningBuffer[1024])
1031 warningBuffer[0] = '\0';
1033 if ( prolog.registerSavedTwiceInCIE == DW_X86_64_RET_ADDR ) {
1034 warningBuffer[0] = '\0'; // silently disable conversion to compact unwind by linker
1035 return UNWIND_X86_64_MODE_DWARF;
1037 // don't create compact unwind info for unsupported dwarf kinds
1038 if ( prolog.registerSavedMoreThanOnce ) {
1039 strcpy(warningBuffer, "register saved more than once (might be shrink wrap)");
1040 return UNWIND_X86_64_MODE_DWARF;
1042 if ( prolog.cfaOffsetWasNegative ) {
1043 strcpy(warningBuffer, "cfa had negative offset (dwarf might contain epilog)");
1044 return UNWIND_X86_64_MODE_DWARF;
1046 if ( prolog.spExtraArgSize != 0 ) {
1047 strcpy(warningBuffer, "dwarf uses DW_CFA_GNU_args_size");
1048 return UNWIND_X86_64_MODE_DWARF;
1050 if ( prolog.sameValueUsed ) {
1051 strcpy(warningBuffer, "dwarf uses DW_CFA_same_value");
1052 return UNWIND_X86_64_MODE_DWARF;
1055 // figure out which kind of frame this function uses
1056 bool standardRBPframe = (
1057 (prolog.cfaRegister == UNW_X86_64_RBP)
1058 && (prolog.cfaRegisterOffset == 16)
1059 && (prolog.savedRegisters[UNW_X86_64_RBP].location == CFI_Parser<A>::kRegisterInCFA)
1060 && (prolog.savedRegisters[UNW_X86_64_RBP].value == -16) );
1061 bool standardRSPframe = (prolog.cfaRegister == UNW_X86_64_RSP);
1062 if ( !standardRBPframe && !standardRSPframe ) {
1063 // no compact encoding for this
1064 strcpy(warningBuffer, "does not use RBP or RSP based frame");
1065 return UNWIND_X86_64_MODE_DWARF;
1068 // scan which registers are saved
1069 int saveRegisterCount = 0;
1070 bool rbxSaved = false;
1071 bool r12Saved = false;
1072 bool r13Saved = false;
1073 bool r14Saved = false;
1074 bool r15Saved = false;
1075 bool rbpSaved = false;
1076 for (int i=0; i < 64; ++i) {
1077 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1078 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterInCFA ) {
1079 sprintf(warningBuffer, "register %d saved somewhere other than in frame", i);
1080 return UNWIND_X86_64_MODE_DWARF;
1083 case UNW_X86_64_RBX:
1085 ++saveRegisterCount;
1087 case UNW_X86_64_R12:
1089 ++saveRegisterCount;
1091 case UNW_X86_64_R13:
1093 ++saveRegisterCount;
1095 case UNW_X86_64_R14:
1097 ++saveRegisterCount;
1099 case UNW_X86_64_R15:
1101 ++saveRegisterCount;
1103 case UNW_X86_64_RBP:
1105 ++saveRegisterCount;
1107 case DW_X86_64_RET_ADDR:
1110 sprintf(warningBuffer, "non-standard register %d being saved in prolog", i);
1111 return UNWIND_X86_64_MODE_DWARF;
1115 const int64_t cfaOffsetRBX = prolog.savedRegisters[UNW_X86_64_RBX].value;
1116 const int64_t cfaOffsetR12 = prolog.savedRegisters[UNW_X86_64_R12].value;
1117 const int64_t cfaOffsetR13 = prolog.savedRegisters[UNW_X86_64_R13].value;
1118 const int64_t cfaOffsetR14 = prolog.savedRegisters[UNW_X86_64_R14].value;
1119 const int64_t cfaOffsetR15 = prolog.savedRegisters[UNW_X86_64_R15].value;
1120 const int64_t cfaOffsetRBP = prolog.savedRegisters[UNW_X86_64_RBP].value;
1122 // encode standard RBP frames
1123 compact_unwind_encoding_t encoding = 0;
1124 if ( standardRBPframe ) {
1126 // +--------------+ <- CFA
1130 // +--------------+ <- rbp
1134 // +--------------+ <- CFA - offset+16
1136 // +--------------+ <- CFA - offset+8
1138 // +--------------+ <- CFA - offset
1144 encoding = UNWIND_X86_64_MODE_RBP_FRAME;
1146 // find save location of farthest register from rbp
1147 int furthestCfaOffset = 0;
1148 if ( rbxSaved & (cfaOffsetRBX < furthestCfaOffset) )
1149 furthestCfaOffset = cfaOffsetRBX;
1150 if ( r12Saved & (cfaOffsetR12 < furthestCfaOffset) )
1151 furthestCfaOffset = cfaOffsetR12;
1152 if ( r13Saved & (cfaOffsetR13 < furthestCfaOffset) )
1153 furthestCfaOffset = cfaOffsetR13;
1154 if ( r14Saved & (cfaOffsetR14 < furthestCfaOffset) )
1155 furthestCfaOffset = cfaOffsetR14;
1156 if ( r15Saved & (cfaOffsetR15 < furthestCfaOffset) )
1157 furthestCfaOffset = cfaOffsetR15;
1159 if ( furthestCfaOffset == 0 ) {
1160 // no registers saved, nothing more to encode
1164 // add stack offset to encoding
1165 int rbpOffset = furthestCfaOffset + 16;
1166 int encodedOffset = rbpOffset/(-8);
1167 if ( encodedOffset > 255 ) {
1168 strcpy(warningBuffer, "offset of saved registers too far to encode");
1169 return UNWIND_X86_64_MODE_DWARF;
1171 encoding |= (encodedOffset << __builtin_ctz(UNWIND_X86_64_RBP_FRAME_OFFSET));
1173 // add register saved from each stack location
1174 bool encodingFailure = false;
1176 encoding |= getRBPEncodedRegister(UNW_X86_64_RBX, cfaOffsetRBX - furthestCfaOffset, encodingFailure);
1178 encoding |= getRBPEncodedRegister(UNW_X86_64_R12, cfaOffsetR12 - furthestCfaOffset, encodingFailure);
1180 encoding |= getRBPEncodedRegister(UNW_X86_64_R13, cfaOffsetR13 - furthestCfaOffset, encodingFailure);
1182 encoding |= getRBPEncodedRegister(UNW_X86_64_R14, cfaOffsetR14 - furthestCfaOffset, encodingFailure);
1184 encoding |= getRBPEncodedRegister(UNW_X86_64_R15, cfaOffsetR15 - furthestCfaOffset, encodingFailure);
1186 if ( encodingFailure ){
1187 strcpy(warningBuffer, "saved registers not contiguous");
1188 return UNWIND_X86_64_MODE_DWARF;
1195 // +--------------+ <- CFA
1199 // +--------------+ <- CFA - 16
1201 // +--------------+ <- CFA - 24
1203 // +--------------+ <- CFA - 32
1205 // +--------------+ <- CFA - 40
1207 // +--------------+ <- CFA - 48
1209 // +--------------+ <- CFA - 56
1214 // for RSP based frames we need to encode stack size in unwind info
1215 encoding = UNWIND_X86_64_MODE_STACK_IMMD;
1216 uint64_t stackValue = prolog.cfaRegisterOffset / 8;
1217 uint32_t stackAdjust = 0;
1218 bool immedStackSize = true;
1219 const uint32_t stackMaxImmedValue = EXTRACT_BITS(0xFFFFFFFF,UNWIND_X86_64_FRAMELESS_STACK_SIZE);
1220 if ( stackValue > stackMaxImmedValue ) {
1221 // stack size is too big to fit as an immediate value, so encode offset of subq instruction in function
1222 if ( prolog.codeOffsetAtStackDecrement == 0 ) {
1223 strcpy(warningBuffer, "stack size is large but stack subq instruction not found");
1224 return UNWIND_X86_64_MODE_DWARF;
1226 pint_t functionContentAdjustStackIns = funcAddr + prolog.codeOffsetAtStackDecrement - 4;
1230 uint32_t stackDecrementInCode = addressSpace.get32(functionContentAdjustStackIns);
1231 stackAdjust = (prolog.cfaRegisterOffset - stackDecrementInCode)/8;
1235 strcpy(warningBuffer, "stack size is large but stack subq instruction not found");
1236 return UNWIND_X86_64_MODE_DWARF;
1239 stackValue = functionContentAdjustStackIns - funcAddr;
1240 immedStackSize = false;
1241 if ( stackAdjust > 7 ) {
1242 strcpy(warningBuffer, "stack subq instruction is too different from dwarf stack size");
1243 return UNWIND_X86_64_MODE_DWARF;
1245 encoding = UNWIND_X86_64_MODE_STACK_IND;
1249 // validate that saved registers are all within 6 slots abutting return address
1251 for (int i=0; i < 6;++i)
1254 if ( cfaOffsetR15 < -56 ) {
1255 strcpy(warningBuffer, "r15 is saved too far from return address");
1256 return UNWIND_X86_64_MODE_DWARF;
1258 registers[(cfaOffsetR15+56)/8] = UNWIND_X86_64_REG_R15;
1261 if ( cfaOffsetR14 < -56 ) {
1262 strcpy(warningBuffer, "r14 is saved too far from return address");
1263 return UNWIND_X86_64_MODE_DWARF;
1265 registers[(cfaOffsetR14+56)/8] = UNWIND_X86_64_REG_R14;
1268 if ( cfaOffsetR13 < -56 ) {
1269 strcpy(warningBuffer, "r13 is saved too far from return address");
1270 return UNWIND_X86_64_MODE_DWARF;
1272 registers[(cfaOffsetR13+56)/8] = UNWIND_X86_64_REG_R13;
1275 if ( cfaOffsetR12 < -56 ) {
1276 strcpy(warningBuffer, "r12 is saved too far from return address");
1277 return UNWIND_X86_64_MODE_DWARF;
1279 registers[(cfaOffsetR12+56)/8] = UNWIND_X86_64_REG_R12;
1282 if ( cfaOffsetRBX < -56 ) {
1283 strcpy(warningBuffer, "rbx is saved too far from return address");
1284 return UNWIND_X86_64_MODE_DWARF;
1286 registers[(cfaOffsetRBX+56)/8] = UNWIND_X86_64_REG_RBX;
1289 if ( cfaOffsetRBP < -56 ) {
1290 strcpy(warningBuffer, "rbp is saved too far from return address");
1291 return UNWIND_X86_64_MODE_DWARF;
1293 registers[(cfaOffsetRBP+56)/8] = UNWIND_X86_64_REG_RBP;
1296 // validate that saved registers are contiguous and abut return address on stack
1297 for (int i=0; i < saveRegisterCount; ++i) {
1298 if ( registers[5-i] == 0 ) {
1299 strcpy(warningBuffer, "registers not save contiguously in stack");
1300 return UNWIND_X86_64_MODE_DWARF;
1304 // encode register permutation
1305 // the 10-bits are encoded differently depending on the number of registers saved
1307 for (int i=6-saveRegisterCount; i < 6; ++i) {
1309 for (int j=6-saveRegisterCount; j < i; ++j) {
1310 if ( registers[j] < registers[i] )
1313 renumregs[i] = registers[i] - countless -1;
1315 uint32_t permutationEncoding = 0;
1316 switch ( saveRegisterCount ) {
1318 permutationEncoding |= (120*renumregs[0] + 24*renumregs[1] + 6*renumregs[2] + 2*renumregs[3] + renumregs[4]);
1321 permutationEncoding |= (120*renumregs[1] + 24*renumregs[2] + 6*renumregs[3] + 2*renumregs[4] + renumregs[5]);
1324 permutationEncoding |= (60*renumregs[2] + 12*renumregs[3] + 3*renumregs[4] + renumregs[5]);
1327 permutationEncoding |= (20*renumregs[3] + 4*renumregs[4] + renumregs[5]);
1330 permutationEncoding |= (5*renumregs[4] + renumregs[5]);
1333 permutationEncoding |= (renumregs[5]);
1337 encoding |= (stackValue << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_SIZE));
1338 encoding |= (stackAdjust << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_ADJUST));
1339 encoding |= (saveRegisterCount << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT));
1340 encoding |= (permutationEncoding << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION));
1349 // x86 specific functions
1351 template <typename A, typename R>
1352 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_x86&)
1354 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)DW_X86_RET_ADDR );
1355 return DW_X86_RET_ADDR;
1358 template <typename A, typename R>
1359 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_x86&)
1361 return (regNum == DW_X86_RET_ADDR);
1364 template <typename A, typename R>
1365 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
1366 const Registers_x86& registers)
1368 if ( prolog.cfaRegister != 0 )
1369 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
1370 else if ( prolog.cfaExpression != 0 )
1371 return evaluateExpression(prolog.cfaExpression, addressSpace, registers, 0);
1373 ABORT("getCFA(): unknown location for x86 cfa");
1380 template <typename A, typename R>
1381 uint32_t DwarfInstructions<A,R>::getEBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure)
1383 if ( (regOffsetFromBaseOffset < 0) || (regOffsetFromBaseOffset > 16) ) {
1387 unsigned int slotIndex = regOffsetFromBaseOffset/4;
1391 return UNWIND_X86_REG_EBX << (slotIndex*3);
1393 return UNWIND_X86_REG_ECX << (slotIndex*3);
1395 return UNWIND_X86_REG_EDX << (slotIndex*3);
1397 return UNWIND_X86_REG_EDI << (slotIndex*3);
1399 return UNWIND_X86_REG_ESI << (slotIndex*3);
1407 template <typename A, typename R>
1408 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
1409 const Registers_x86& r, const typename CFI_Parser<A>::PrologInfo& prolog,
1410 char warningBuffer[1024])
1412 warningBuffer[0] = '\0';
1414 if ( prolog.registerSavedTwiceInCIE == DW_X86_RET_ADDR ) {
1415 warningBuffer[0] = '\0'; // silently disable conversion to compact unwind by linker
1416 return UNWIND_X86_64_MODE_DWARF;
1418 // don't create compact unwind info for unsupported dwarf kinds
1419 if ( prolog.registerSavedMoreThanOnce ) {
1420 strcpy(warningBuffer, "register saved more than once (might be shrink wrap)");
1421 return UNWIND_X86_MODE_DWARF;
1423 if ( prolog.spExtraArgSize != 0 ) {
1424 strcpy(warningBuffer, "dwarf uses DW_CFA_GNU_args_size");
1425 return UNWIND_X86_MODE_DWARF;
1427 if ( prolog.sameValueUsed ) {
1428 strcpy(warningBuffer, "dwarf uses DW_CFA_same_value");
1429 return UNWIND_X86_MODE_DWARF;
1432 // figure out which kind of frame this function uses
1433 bool standardEBPframe = (
1434 (prolog.cfaRegister == UNW_X86_EBP)
1435 && (prolog.cfaRegisterOffset == 8)
1436 && (prolog.savedRegisters[UNW_X86_EBP].location == CFI_Parser<A>::kRegisterInCFA)
1437 && (prolog.savedRegisters[UNW_X86_EBP].value == -8) );
1438 bool standardESPframe = (prolog.cfaRegister == UNW_X86_ESP);
1439 if ( !standardEBPframe && !standardESPframe ) {
1440 // no compact encoding for this
1441 strcpy(warningBuffer, "does not use EBP or ESP based frame");
1442 return UNWIND_X86_MODE_DWARF;
1445 // scan which registers are saved
1446 int saveRegisterCount = 0;
1447 bool ebxSaved = false;
1448 bool ecxSaved = false;
1449 bool edxSaved = false;
1450 bool esiSaved = false;
1451 bool ediSaved = false;
1452 bool ebpSaved = false;
1453 for (int i=0; i < 64; ++i) {
1454 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1455 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterInCFA ) {
1456 sprintf(warningBuffer, "register %d saved somewhere other than in frame", i);
1457 return UNWIND_X86_MODE_DWARF;
1462 ++saveRegisterCount;
1466 ++saveRegisterCount;
1470 ++saveRegisterCount;
1474 ++saveRegisterCount;
1478 ++saveRegisterCount;
1482 ++saveRegisterCount;
1484 case DW_X86_RET_ADDR:
1487 sprintf(warningBuffer, "non-standard register %d being saved in prolog", i);
1488 return UNWIND_X86_MODE_DWARF;
1492 const int32_t cfaOffsetEBX = prolog.savedRegisters[UNW_X86_EBX].value;
1493 const int32_t cfaOffsetECX = prolog.savedRegisters[UNW_X86_ECX].value;
1494 const int32_t cfaOffsetEDX = prolog.savedRegisters[UNW_X86_EDX].value;
1495 const int32_t cfaOffsetEDI = prolog.savedRegisters[UNW_X86_EDI].value;
1496 const int32_t cfaOffsetESI = prolog.savedRegisters[UNW_X86_ESI].value;
1497 const int32_t cfaOffsetEBP = prolog.savedRegisters[UNW_X86_EBP].value;
1499 // encode standard RBP frames
1500 compact_unwind_encoding_t encoding = 0;
1501 if ( standardEBPframe ) {
1503 // +--------------+ <- CFA
1507 // +--------------+ <- ebp
1511 // +--------------+ <- CFA - offset+8
1513 // +--------------+ <- CFA - offset+e
1515 // +--------------+ <- CFA - offset
1521 encoding = UNWIND_X86_MODE_EBP_FRAME;
1523 // find save location of farthest register from ebp
1524 int furthestCfaOffset = 0;
1525 if ( ebxSaved & (cfaOffsetEBX < furthestCfaOffset) )
1526 furthestCfaOffset = cfaOffsetEBX;
1527 if ( ecxSaved & (cfaOffsetECX < furthestCfaOffset) )
1528 furthestCfaOffset = cfaOffsetECX;
1529 if ( edxSaved & (cfaOffsetEDX < furthestCfaOffset) )
1530 furthestCfaOffset = cfaOffsetEDX;
1531 if ( ediSaved & (cfaOffsetEDI < furthestCfaOffset) )
1532 furthestCfaOffset = cfaOffsetEDI;
1533 if ( esiSaved & (cfaOffsetESI < furthestCfaOffset) )
1534 furthestCfaOffset = cfaOffsetESI;
1536 if ( furthestCfaOffset == 0 ) {
1537 // no registers saved, nothing more to encode
1541 // add stack offset to encoding
1542 int ebpOffset = furthestCfaOffset + 8;
1543 int encodedOffset = ebpOffset/(-4);
1544 if ( encodedOffset > 255 ) {
1545 strcpy(warningBuffer, "offset of saved registers too far to encode");
1546 return UNWIND_X86_MODE_DWARF;
1548 encoding |= (encodedOffset << __builtin_ctz(UNWIND_X86_EBP_FRAME_OFFSET));
1550 // add register saved from each stack location
1551 bool encodingFailure = false;
1553 encoding |= getEBPEncodedRegister(UNW_X86_EBX, cfaOffsetEBX - furthestCfaOffset, encodingFailure);
1555 encoding |= getEBPEncodedRegister(UNW_X86_ECX, cfaOffsetECX - furthestCfaOffset, encodingFailure);
1557 encoding |= getEBPEncodedRegister(UNW_X86_EDX, cfaOffsetEDX - furthestCfaOffset, encodingFailure);
1559 encoding |= getEBPEncodedRegister(UNW_X86_EDI, cfaOffsetEDI - furthestCfaOffset, encodingFailure);
1561 encoding |= getEBPEncodedRegister(UNW_X86_ESI, cfaOffsetESI - furthestCfaOffset, encodingFailure);
1563 if ( encodingFailure ){
1564 strcpy(warningBuffer, "saved registers not contiguous");
1565 return UNWIND_X86_MODE_DWARF;
1572 // +--------------+ <- CFA
1576 // +--------------+ <- CFA - 8
1578 // +--------------+ <- CFA - 12
1580 // +--------------+ <- CFA - 16
1582 // +--------------+ <- CFA - 20
1584 // +--------------+ <- CFA - 24
1586 // +--------------+ <- CFA - 28
1591 // for ESP based frames we need to encode stack size in unwind info
1592 encoding = UNWIND_X86_MODE_STACK_IMMD;
1593 uint64_t stackValue = prolog.cfaRegisterOffset / 4;
1594 uint32_t stackAdjust = 0;
1595 bool immedStackSize = true;
1596 const uint32_t stackMaxImmedValue = EXTRACT_BITS(0xFFFFFFFF,UNWIND_X86_FRAMELESS_STACK_SIZE);
1597 if ( stackValue > stackMaxImmedValue ) {
1598 // stack size is too big to fit as an immediate value, so encode offset of subq instruction in function
1599 pint_t functionContentAdjustStackIns = funcAddr + prolog.codeOffsetAtStackDecrement - 4;
1603 uint32_t stackDecrementInCode = addressSpace.get32(functionContentAdjustStackIns);
1604 stackAdjust = (prolog.cfaRegisterOffset - stackDecrementInCode)/4;
1608 strcpy(warningBuffer, "stack size is large but stack subl instruction not found");
1609 return UNWIND_X86_MODE_DWARF;
1612 stackValue = functionContentAdjustStackIns - funcAddr;
1613 immedStackSize = false;
1614 if ( stackAdjust > 7 ) {
1615 strcpy(warningBuffer, "stack subl instruction is too different from dwarf stack size");
1616 return UNWIND_X86_MODE_DWARF;
1618 encoding = UNWIND_X86_MODE_STACK_IND;
1622 // validate that saved registers are all within 6 slots abutting return address
1624 for (int i=0; i < 6;++i)
1627 if ( cfaOffsetEBX < -28 ) {
1628 strcpy(warningBuffer, "ebx is saved too far from return address");
1629 return UNWIND_X86_MODE_DWARF;
1631 registers[(cfaOffsetEBX+28)/4] = UNWIND_X86_REG_EBX;
1634 if ( cfaOffsetECX < -28 ) {
1635 strcpy(warningBuffer, "ecx is saved too far from return address");
1636 return UNWIND_X86_MODE_DWARF;
1638 registers[(cfaOffsetECX+28)/4] = UNWIND_X86_REG_ECX;
1641 if ( cfaOffsetEDX < -28 ) {
1642 strcpy(warningBuffer, "edx is saved too far from return address");
1643 return UNWIND_X86_MODE_DWARF;
1645 registers[(cfaOffsetEDX+28)/4] = UNWIND_X86_REG_EDX;
1648 if ( cfaOffsetEDI < -28 ) {
1649 strcpy(warningBuffer, "edi is saved too far from return address");
1650 return UNWIND_X86_MODE_DWARF;
1652 registers[(cfaOffsetEDI+28)/4] = UNWIND_X86_REG_EDI;
1655 if ( cfaOffsetESI < -28 ) {
1656 strcpy(warningBuffer, "esi is saved too far from return address");
1657 return UNWIND_X86_MODE_DWARF;
1659 registers[(cfaOffsetESI+28)/4] = UNWIND_X86_REG_ESI;
1662 if ( cfaOffsetEBP < -28 ) {
1663 strcpy(warningBuffer, "ebp is saved too far from return address");
1664 return UNWIND_X86_MODE_DWARF;
1666 registers[(cfaOffsetEBP+28)/4] = UNWIND_X86_REG_EBP;
1669 // validate that saved registers are contiguous and abut return address on stack
1670 for (int i=0; i < saveRegisterCount; ++i) {
1671 if ( registers[5-i] == 0 ) {
1672 strcpy(warningBuffer, "registers not save contiguously in stack");
1673 return UNWIND_X86_MODE_DWARF;
1677 // encode register permutation
1678 // the 10-bits are encoded differently depending on the number of registers saved
1680 for (int i=6-saveRegisterCount; i < 6; ++i) {
1682 for (int j=6-saveRegisterCount; j < i; ++j) {
1683 if ( registers[j] < registers[i] )
1686 renumregs[i] = registers[i] - countless -1;
1688 uint32_t permutationEncoding = 0;
1689 switch ( saveRegisterCount ) {
1691 permutationEncoding |= (120*renumregs[0] + 24*renumregs[1] + 6*renumregs[2] + 2*renumregs[3] + renumregs[4]);
1694 permutationEncoding |= (120*renumregs[1] + 24*renumregs[2] + 6*renumregs[3] + 2*renumregs[4] + renumregs[5]);
1697 permutationEncoding |= (60*renumregs[2] + 12*renumregs[3] + 3*renumregs[4] + renumregs[5]);
1700 permutationEncoding |= (20*renumregs[3] + 4*renumregs[4] + renumregs[5]);
1703 permutationEncoding |= (5*renumregs[4] + renumregs[5]);
1706 permutationEncoding |= (renumregs[5]);
1710 encoding |= (stackValue << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_SIZE));
1711 encoding |= (stackAdjust << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_ADJUST));
1712 encoding |= (saveRegisterCount << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_REG_COUNT));
1713 encoding |= (permutationEncoding << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION));
1725 // ppc specific functions
1727 template <typename A, typename R>
1728 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_ppc&)
1730 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)UNW_PPC_SPEFSCR );
1731 return UNW_PPC_SPEFSCR;
1734 template <typename A, typename R>
1735 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_ppc&)
1737 return (regNum == UNW_PPC_LR);
1740 template <typename A, typename R>
1741 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
1742 const Registers_ppc& registers)
1744 if ( prolog.cfaRegister != 0 )
1745 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
1746 else if ( prolog.cfaExpression != 0 )
1747 return evaluateExpression(prolog.cfaExpression, addressSpace, registers, 0);
1749 ABORT("getCFA(): unknown location for ppc cfa");
1753 template <typename A, typename R>
1754 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_ppc&)
1756 return UNWIND_X86_MODE_DWARF;
1760 template <typename A, typename R>
1761 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
1762 const Registers_ppc& r, const typename CFI_Parser<A>::PrologInfo& prolog,
1763 char warningBuffer[1024])
1765 warningBuffer[0] = '\0';
1766 return UNWIND_X86_MODE_DWARF;
1772 // arm64 specific functions
1775 template <typename A, typename R>
1776 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_arm64&)
1778 return UNWIND_ARM64_MODE_DWARF;
1781 template <typename A, typename R>
1782 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_arm64&)
1784 return (regNum == UNW_ARM64_LR);
1787 template <typename A, typename R>
1788 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_arm64&)
1790 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)UNW_ARM64_D31 );
1791 return UNW_ARM64_D31;
1795 template <typename A, typename R>
1796 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
1797 const Registers_arm64& registers)
1799 if ( prolog.cfaRegister != 0 )
1800 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
1802 ABORT("getCFA(): unsupported location for arm64 cfa");
1805 template <typename A, typename R>
1806 bool DwarfInstructions<A,R>::checkRegisterPair(uint32_t reg, const typename CFI_Parser<A>::PrologInfo& prolog,
1807 int& offset, char warningBuffer[1024])
1809 if ( (prolog.savedRegisters[reg].location != CFI_Parser<A>::kRegisterUnused)
1810 || (prolog.savedRegisters[reg+1].location != CFI_Parser<A>::kRegisterUnused) ) {
1811 if ( prolog.savedRegisters[reg].location != CFI_Parser<A>::kRegisterInCFA ) {
1812 sprintf(warningBuffer, "register %d saved somewhere other than in frame", reg);
1815 if ( prolog.savedRegisters[reg+1].location != CFI_Parser<A>::kRegisterInCFA ) {
1816 sprintf(warningBuffer, "register %d saved somewhere other than in frame", reg+1);
1819 if ( prolog.savedRegisters[reg].value != prolog.savedRegisters[reg+1].value + 8 ) {
1820 sprintf(warningBuffer, "registers %d and %d not saved contiguously in frame", reg, reg+1);
1823 if ( prolog.savedRegisters[reg].value != offset ) {
1824 sprintf(warningBuffer, "registers %d not saved contiguously in frame", reg);
1834 template <typename A, typename R>
1835 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
1836 const Registers_arm64& r, const typename CFI_Parser<A>::PrologInfo& prolog,
1837 char warningBuffer[1024])
1839 warningBuffer[0] = '\0';
1841 if ( prolog.registerSavedTwiceInCIE == UNW_ARM64_LR ) {
1842 warningBuffer[0] = '\0'; // silently disable conversion to compact unwind by linker
1843 return UNWIND_ARM64_MODE_DWARF;
1845 // don't create compact unwind info for unsupported dwarf kinds
1846 if ( prolog.registerSavedMoreThanOnce ) {
1847 strcpy(warningBuffer, "register saved more than once (might be shrink wrap)");
1848 return UNWIND_ARM64_MODE_DWARF;
1850 if ( prolog.spExtraArgSize != 0 ) {
1851 strcpy(warningBuffer, "dwarf uses DW_CFA_GNU_args_size");
1852 return UNWIND_ARM64_MODE_DWARF;
1854 if ( prolog.sameValueUsed ) {
1855 strcpy(warningBuffer, "dwarf uses DW_CFA_same_value");
1856 return UNWIND_ARM64_MODE_DWARF;
1859 compact_unwind_encoding_t encoding = 0;
1862 // figure out which kind of frame this function uses
1863 bool standardFPframe = (
1864 (prolog.cfaRegister == UNW_ARM64_FP)
1865 && (prolog.cfaRegisterOffset == 16)
1866 && (prolog.savedRegisters[UNW_ARM64_FP].location == CFI_Parser<A>::kRegisterInCFA)
1867 && (prolog.savedRegisters[UNW_ARM64_FP].value == -16)
1868 && (prolog.savedRegisters[UNW_ARM64_LR].location == CFI_Parser<A>::kRegisterInCFA)
1869 && (prolog.savedRegisters[UNW_ARM64_LR].value == -8) );
1871 bool standardFrameless = ( prolog.cfaRegister == UNW_ARM64_SP );
1873 if ( standardFrameless ) {
1874 // verify enough space for registers saved
1876 for (int i=0; i < 96; ++i) {
1877 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused )
1880 if ( count * 8 > prolog.cfaRegisterOffset ) {
1881 strcpy(warningBuffer, "saved registers do not fit in stack size");
1882 return UNWIND_ARM64_MODE_DWARF;
1884 if ( (prolog.cfaRegisterOffset % 16) != 0 ) {
1885 strcpy(warningBuffer, "stack size is not 16-byte multiple");
1886 return UNWIND_ARM64_MODE_DWARF;
1888 const int32_t maxStack = (UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK >> __builtin_ctz(UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK));
1889 if ( (prolog.cfaRegisterOffset / 16) > maxStack ) {
1890 strcpy(warningBuffer, "stack size is too large for frameless function");
1891 return UNWIND_ARM64_MODE_DWARF;
1893 encoding = UNWIND_ARM64_MODE_FRAMELESS | ((prolog.cfaRegisterOffset/16) << __builtin_ctz(UNWIND_ARM64_FRAMELESS_STACK_SIZE_MASK));
1896 else if ( standardFPframe ) {
1897 encoding = UNWIND_ARM64_MODE_FRAME;
1901 // no compact encoding for this
1902 strcpy(warningBuffer, "does not use standard frame");
1903 return UNWIND_ARM64_MODE_DWARF;
1906 // make sure no volatile registers are saved
1907 for (int i=UNW_ARM64_X0; i < UNW_ARM64_X19; ++i) {
1908 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1909 sprintf(warningBuffer, "non standard register %d saved in frame", i);
1910 return UNWIND_ARM64_MODE_DWARF;
1913 for (int i=UNW_ARM64_SP+1; i < UNW_ARM64_D8; ++i) {
1914 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1915 sprintf(warningBuffer, "non standard register %d saved in frame", i);
1916 return UNWIND_ARM64_MODE_DWARF;
1919 for (int i=UNW_ARM64_D16; i < UNW_ARM64_D31+1; ++i) {
1920 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1921 sprintf(warningBuffer, "non standard register %d saved in frame", i);
1922 return UNWIND_ARM64_MODE_DWARF;
1927 bool X19_X20_saved = checkRegisterPair(UNW_ARM64_X19, prolog, offset, warningBuffer);
1928 bool X21_X22_saved = checkRegisterPair(UNW_ARM64_X21, prolog, offset, warningBuffer);
1929 bool X23_X24_saved = checkRegisterPair(UNW_ARM64_X23, prolog, offset, warningBuffer);
1930 bool X25_X26_saved = checkRegisterPair(UNW_ARM64_X25, prolog, offset, warningBuffer);
1931 bool X27_X28_saved = checkRegisterPair(UNW_ARM64_X27, prolog, offset, warningBuffer);
1932 bool D8_D9_saved = checkRegisterPair(UNW_ARM64_D8, prolog, offset, warningBuffer);
1933 bool D10_D11_saved = checkRegisterPair(UNW_ARM64_D10, prolog, offset, warningBuffer);
1934 bool D12_D13_saved = checkRegisterPair(UNW_ARM64_D12, prolog, offset, warningBuffer);
1935 bool D14_D15_saved = checkRegisterPair(UNW_ARM64_D14, prolog, offset, warningBuffer);
1936 if ( warningBuffer[0] != '\0' )
1937 return UNWIND_ARM64_MODE_DWARF;
1939 if ( X19_X20_saved )
1940 encoding |= UNWIND_ARM64_FRAME_X19_X20_PAIR;
1941 if ( X21_X22_saved )
1942 encoding |= UNWIND_ARM64_FRAME_X21_X22_PAIR;
1943 if ( X23_X24_saved )
1944 encoding |= UNWIND_ARM64_FRAME_X23_X24_PAIR;
1945 if ( X25_X26_saved )
1946 encoding |= UNWIND_ARM64_FRAME_X25_X26_PAIR;
1947 if ( X27_X28_saved )
1948 encoding |= UNWIND_ARM64_FRAME_X27_X28_PAIR;
1950 encoding |= UNWIND_ARM64_FRAME_D8_D9_PAIR;
1951 if ( D10_D11_saved )
1952 encoding |= UNWIND_ARM64_FRAME_D10_D11_PAIR;
1953 if ( D12_D13_saved )
1954 encoding |= UNWIND_ARM64_FRAME_D12_D13_PAIR;
1955 if ( D14_D15_saved )
1956 encoding |= UNWIND_ARM64_FRAME_D14_D15_PAIR;
1961 } // namespace libunwind
1964 #endif // __DWARF_INSTRUCTIONS_HPP__