1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008 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 CFI_Atom_Info<A>* infos, uint32_t infosCount, void* ref, WarnFunc warn);
101 static compact_unwind_encoding_t createCompactEncodingFromFDE(A& addressSpace, pint_t fdeStart,
102 pint_t* lsda, pint_t* personality,
103 char warningBuffer[1024]);
105 static int stepWithDwarf(A& addressSpace, pint_t pc, pint_t fdeStart, R& registers);
110 DW_X86_64_RET_ADDR = 16
117 static pint_t evaluateExpression(pint_t expression, A& addressSpace, const R& registers, pint_t initialStackValue);
118 static pint_t getSavedRegister(A& addressSpace, const R& registers, pint_t cfa,
119 const typename CFI_Parser<A>::RegisterLocation& savedReg);
120 static double getSavedFloatRegister(A& addressSpace, const R& registers, pint_t cfa,
121 const typename CFI_Parser<A>::RegisterLocation& savedReg);
122 static v128 getSavedVectorRegister(A& addressSpace, const R& registers, pint_t cfa,
123 const typename CFI_Parser<A>::RegisterLocation& savedReg);
125 // x86 specific variants
126 static int lastRestoreReg(const Registers_x86&);
127 static bool isReturnAddressRegister(int regNum, const Registers_x86&);
128 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_x86&);
130 static uint32_t getEBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure);
131 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_x86&);
132 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
133 const Registers_x86&, const typename CFI_Parser<A>::PrologInfo& prolog,
134 char warningBuffer[1024]);
136 // x86_64 specific variants
137 static int lastRestoreReg(const Registers_x86_64&);
138 static bool isReturnAddressRegister(int regNum, const Registers_x86_64&);
139 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_x86_64&);
141 static uint32_t getRBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure);
142 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_x86_64&);
143 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
144 const Registers_x86_64&, const typename CFI_Parser<A>::PrologInfo& prolog,
145 char warningBuffer[1024]);
147 // ppc specific variants
148 static int lastRestoreReg(const Registers_ppc&);
149 static bool isReturnAddressRegister(int regNum, const Registers_ppc&);
150 static pint_t getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog, const Registers_ppc&);
151 static compact_unwind_encoding_t encodeToUseDwarf(const Registers_ppc&);
152 static compact_unwind_encoding_t createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
153 const Registers_ppc&, const typename CFI_Parser<A>::PrologInfo& prolog,
154 char warningBuffer[1024]);
160 template <typename A, typename R>
161 const char* DwarfInstructions<A,R>::parseCFIs(A& addressSpace, pint_t ehSectionStart, uint32_t sectionLength,
162 CFI_Atom_Info<A>* infos, uint32_t infosCount, void* ref, WarnFunc warn)
164 typename CFI_Parser<A>::CIE_Info cieInfo;
165 CFI_Atom_Info<A>* entry = infos;
166 CFI_Atom_Info<A>* end = &infos[infosCount];
167 const pint_t ehSectionEnd = ehSectionStart + sectionLength;
168 for (pint_t p=ehSectionStart; p < ehSectionEnd; ) {
169 pint_t currentCFI = p;
170 uint64_t cfiLength = addressSpace.get32(p);
172 if ( cfiLength == 0xffffffff ) {
173 // 0xffffffff means length is really next 8 bytes
174 cfiLength = addressSpace.get64(p);
177 if ( cfiLength == 0 )
178 return NULL; // end marker
180 return "too little space allocated for parseCFIs";
181 pint_t nextCFI = p + cfiLength;
182 uint32_t id = addressSpace.get32(p);
185 const char* err = CFI_Parser<A>::parseCIE(addressSpace, currentCFI, &cieInfo);
188 entry->address = currentCFI;
189 entry->size = nextCFI - currentCFI;
191 entry->u.cieInfo.personality.targetAddress = cieInfo.personality;
192 entry->u.cieInfo.personality.offsetInCFI = cieInfo.personalityOffsetInCIE;
193 entry->u.cieInfo.personality.encodingOfTargetAddress = cieInfo.personalityEncoding;
198 entry->address = currentCFI;
199 entry->size = nextCFI - currentCFI;
200 entry->isCIE = false;
201 entry->u.fdeInfo.function.targetAddress = CFI_INVALID_ADDRESS;
202 entry->u.fdeInfo.cie.targetAddress = CFI_INVALID_ADDRESS;
203 entry->u.fdeInfo.lsda.targetAddress = CFI_INVALID_ADDRESS;
204 uint32_t ciePointer = addressSpace.get32(p);
205 pint_t cieStart = p-ciePointer;
206 // validate pointer to CIE is within section
207 if ( (cieStart < ehSectionStart) || (cieStart > ehSectionEnd) )
208 return "FDE points to CIE outside __eh_frame section";
209 // optimize usual case where cie is same for all FDEs
210 if ( cieStart != cieInfo.cieStart ) {
211 const char* err = CFI_Parser<A>::parseCIE(addressSpace, cieStart, &cieInfo);
215 entry->u.fdeInfo.cie.targetAddress = cieStart;
216 entry->u.fdeInfo.cie.offsetInCFI = p-currentCFI;
217 entry->u.fdeInfo.cie.encodingOfTargetAddress = DW_EH_PE_sdata4 | DW_EH_PE_pcrel;
219 // parse pc begin and range
220 pint_t offsetOfFunctionAddress = p-currentCFI;
221 pint_t pcStart = addressSpace.getEncodedP(p, nextCFI, cieInfo.pointerEncoding);
222 pint_t pcRange = addressSpace.getEncodedP(p, nextCFI, cieInfo.pointerEncoding & 0x0F);
223 //fprintf(stderr, "FDE with pcRange [0x%08llX, 0x%08llX)\n",(uint64_t)pcStart, (uint64_t)(pcStart+pcRange));
224 // test if pc is within the function this FDE covers
225 entry->u.fdeInfo.function.targetAddress = pcStart;
226 entry->u.fdeInfo.function.offsetInCFI = offsetOfFunctionAddress;
227 entry->u.fdeInfo.function.encodingOfTargetAddress = cieInfo.pointerEncoding;
228 // check for augmentation length
229 if ( cieInfo.fdesHaveAugmentationData ) {
230 uintptr_t augLen = addressSpace.getULEB128(p, nextCFI);
231 pint_t endOfAug = p + augLen;
232 if ( cieInfo.lsdaEncoding != 0 ) {
233 // peek at value (without indirection). Zero means no lsda
234 pint_t lsdaStart = p;
235 if ( addressSpace.getEncodedP(p, nextCFI, cieInfo.lsdaEncoding & 0x0F) != 0 ) {
236 // reset pointer and re-parse lsda address
238 pint_t offsetOfLSDAAddress = p-currentCFI;
239 entry->u.fdeInfo.lsda.targetAddress = addressSpace.getEncodedP(p, nextCFI, cieInfo.lsdaEncoding);
240 entry->u.fdeInfo.lsda.offsetInCFI = offsetOfLSDAAddress;
241 entry->u.fdeInfo.lsda.encodingOfTargetAddress = cieInfo.lsdaEncoding;
246 // compute compact unwind encoding
247 typename CFI_Parser<A>::FDE_Info fdeInfo;
248 fdeInfo.fdeStart = currentCFI;
249 fdeInfo.fdeLength = nextCFI - currentCFI;
250 fdeInfo.fdeInstructions = p;
251 fdeInfo.pcStart = pcStart;
252 fdeInfo.pcEnd = pcStart + pcRange;
253 fdeInfo.lsda = entry->u.fdeInfo.lsda.targetAddress;
254 typename CFI_Parser<A>::PrologInfo prolog;
255 R dummy; // for proper selection of architecture specific functions
256 if ( CFI_Parser<A>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo, CFI_INVALID_ADDRESS, &prolog) ) {
257 char warningBuffer[1024];
258 entry->u.fdeInfo.compactUnwindInfo = createCompactEncodingFromProlog(addressSpace, fdeInfo.pcStart, dummy, prolog, warningBuffer);
259 if ( fdeInfo.lsda != CFI_INVALID_ADDRESS )
260 entry->u.fdeInfo.compactUnwindInfo |= UNWIND_HAS_LSDA;
261 if ( warningBuffer[0] != '\0' )
262 warn(ref, fdeInfo.pcStart, warningBuffer);
265 warn(ref, CFI_INVALID_ADDRESS, "dwarf unwind instructions could not be parsed");
266 entry->u.fdeInfo.compactUnwindInfo = encodeToUseDwarf(dummy);
273 return "wrong entry count for parseCFIs";
274 return NULL; // success
280 template <typename A, typename R>
281 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromFDE(A& addressSpace, pint_t fdeStart,
282 pint_t* lsda, pint_t* personality,
283 char warningBuffer[1024])
285 typename CFI_Parser<A>::FDE_Info fdeInfo;
286 typename CFI_Parser<A>::CIE_Info cieInfo;
287 R dummy; // for proper selection of architecture specific functions
288 if ( CFI_Parser<A>::decodeFDE(addressSpace, fdeStart, &fdeInfo, &cieInfo) == NULL ) {
289 typename CFI_Parser<A>::PrologInfo prolog;
290 if ( CFI_Parser<A>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo, CFI_INVALID_ADDRESS, &prolog) ) {
291 *lsda = fdeInfo.lsda;
292 *personality = cieInfo.personality;
293 compact_unwind_encoding_t encoding;
294 encoding = createCompactEncodingFromProlog(addressSpace, fdeInfo.pcStart, dummy, prolog, warningBuffer);
295 if ( fdeInfo.lsda != 0 )
296 encoding |= UNWIND_HAS_LSDA;
300 strcpy(warningBuffer, "dwarf unwind instructions could not be parsed");
301 return encodeToUseDwarf(dummy);
305 strcpy(warningBuffer, "dwarf FDE could not be parsed");
306 return encodeToUseDwarf(dummy);
311 template <typename A, typename R>
312 typename A::pint_t DwarfInstructions<A,R>::getSavedRegister(A& addressSpace, const R& registers, pint_t cfa,
313 const typename CFI_Parser<A>::RegisterLocation& savedReg)
315 switch ( savedReg.location ) {
316 case CFI_Parser<A>::kRegisterInCFA:
317 return addressSpace.getP(cfa + savedReg.value);
319 case CFI_Parser<A>::kRegisterAtExpression:
320 return addressSpace.getP(evaluateExpression(savedReg.value, addressSpace, registers, cfa));
322 case CFI_Parser<A>::kRegisterIsExpression:
323 return evaluateExpression(savedReg.value, addressSpace, registers, cfa);
325 case CFI_Parser<A>::kRegisterInRegister:
326 return registers.getRegister(savedReg.value);
328 case CFI_Parser<A>::kRegisterUnused:
329 case CFI_Parser<A>::kRegisterOffsetFromCFA:
333 ABORT("unsupported restore location for register");
336 template <typename A, typename R>
337 double DwarfInstructions<A,R>::getSavedFloatRegister(A& addressSpace, const R& registers, pint_t cfa,
338 const typename CFI_Parser<A>::RegisterLocation& savedReg)
340 switch ( savedReg.location ) {
341 case CFI_Parser<A>::kRegisterInCFA:
342 return addressSpace.getDouble(cfa + savedReg.value);
344 case CFI_Parser<A>::kRegisterAtExpression:
345 return addressSpace.getDouble(evaluateExpression(savedReg.value, addressSpace, registers, cfa));
347 case CFI_Parser<A>::kRegisterIsExpression:
348 case CFI_Parser<A>::kRegisterUnused:
349 case CFI_Parser<A>::kRegisterOffsetFromCFA:
350 case CFI_Parser<A>::kRegisterInRegister:
354 ABORT("unsupported restore location for float register");
357 template <typename A, typename R>
358 v128 DwarfInstructions<A,R>::getSavedVectorRegister(A& addressSpace, const R& registers, pint_t cfa,
359 const typename CFI_Parser<A>::RegisterLocation& savedReg)
361 switch ( savedReg.location ) {
362 case CFI_Parser<A>::kRegisterInCFA:
363 return addressSpace.getVector(cfa + savedReg.value);
365 case CFI_Parser<A>::kRegisterAtExpression:
366 return addressSpace.getVector(evaluateExpression(savedReg.value, addressSpace, registers, cfa));
368 case CFI_Parser<A>::kRegisterIsExpression:
369 case CFI_Parser<A>::kRegisterUnused:
370 case CFI_Parser<A>::kRegisterOffsetFromCFA:
371 case CFI_Parser<A>::kRegisterInRegister:
375 ABORT("unsupported restore location for vector register");
379 template <typename A, typename R>
380 int DwarfInstructions<A,R>::stepWithDwarf(A& addressSpace, pint_t pc, pint_t fdeStart, R& registers)
382 //fprintf(stderr, "stepWithDwarf(pc=0x%0llX, fdeStart=0x%0llX)\n", (uint64_t)pc, (uint64_t)fdeStart);
383 typename CFI_Parser<A>::FDE_Info fdeInfo;
384 typename CFI_Parser<A>::CIE_Info cieInfo;
385 if ( CFI_Parser<A>::decodeFDE(addressSpace, fdeStart, &fdeInfo, &cieInfo) == NULL ) {
386 typename CFI_Parser<A>::PrologInfo prolog;
387 if ( CFI_Parser<A>::parseFDEInstructions(addressSpace, fdeInfo, cieInfo, pc, &prolog) ) {
388 R newRegisters = registers;
390 // get pointer to cfa (architecture specific)
391 pint_t cfa = getCFA(addressSpace, prolog, registers);
393 // restore registers that dwarf says were saved
394 pint_t returnAddress = 0;
395 for (int i=0; i <= lastRestoreReg(newRegisters); ++i) {
396 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
397 if ( registers.validFloatRegister(i) )
398 newRegisters.setFloatRegister(i, getSavedFloatRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]));
399 else if ( registers.validVectorRegister(i) )
400 newRegisters.setVectorRegister(i, getSavedVectorRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]));
401 else if ( isReturnAddressRegister(i, registers) )
402 returnAddress = getSavedRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]);
403 else if ( registers.validRegister(i) )
404 newRegisters.setRegister(i, getSavedRegister(addressSpace, registers, cfa, prolog.savedRegisters[i]));
410 // by definition the CFA is the stack pointer at the call site, so restoring SP means setting it to CFA
411 newRegisters.setSP(cfa);
413 // return address is address after call site instruction, so setting IP to that does a return
414 newRegisters.setIP(returnAddress);
416 // do the actual step by replacing the register set with the new ones
417 registers = newRegisters;
419 return UNW_STEP_SUCCESS;
422 return UNW_EBADFRAME;
427 template <typename A, typename R>
428 typename A::pint_t DwarfInstructions<A,R>::evaluateExpression(pint_t expression, A& addressSpace,
429 const R& registers, pint_t initialStackValue)
431 const bool log = false;
432 pint_t p = expression;
433 pint_t expressionEnd = expression+20; // just need something until length is read
434 uint64_t length = addressSpace.getULEB128(p, expressionEnd);
435 expressionEnd = p + length;
436 if (log) fprintf(stderr, "evaluateExpression(): length=%llu\n", length);
439 *(++sp) = initialStackValue;
441 while ( p < expressionEnd ) {
443 for(pint_t* t = sp; t > stack; --t) {
444 fprintf(stderr, "sp[] = 0x%llX\n", (uint64_t)(*t));
447 uint8_t opcode = addressSpace.get8(p++);
453 // push immediate address sized value
454 value = addressSpace.getP(p);
457 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
461 // pop stack, dereference, push result
463 *(++sp) = addressSpace.getP(value);
464 if (log) fprintf(stderr, "dereference 0x%llX\n", (uint64_t)value);
468 // push immediate 1 byte value
469 value = addressSpace.get8(p);
472 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
476 // push immediate 1 byte signed value
477 svalue = (int8_t)addressSpace.get8(p);
480 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
484 // push immediate 2 byte value
485 value = addressSpace.get16(p);
488 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
492 // push immediate 2 byte signed value
493 svalue = (int16_t)addressSpace.get16(p);
496 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
500 // push immediate 4 byte value
501 value = addressSpace.get32(p);
504 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
508 // push immediate 4 byte signed value
509 svalue = (int32_t)addressSpace.get32(p);
512 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
516 // push immediate 8 byte value
517 value = addressSpace.get64(p);
520 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
524 // push immediate 8 byte signed value
525 value = (int32_t)addressSpace.get64(p);
528 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
532 // push immediate ULEB128 value
533 value = addressSpace.getULEB128(p, expressionEnd);
535 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)value);
539 // push immediate SLEB128 value
540 svalue = addressSpace.getSLEB128(p, expressionEnd);
542 if (log) fprintf(stderr, "push 0x%llX\n", (uint64_t)svalue);
549 if (log) fprintf(stderr, "duplicate top of stack\n");
555 if (log) fprintf(stderr, "pop top of stack\n");
562 if (log) fprintf(stderr, "duplicate second in stack\n");
567 reg = addressSpace.get8(p);
571 if (log) fprintf(stderr, "duplicate %d in stack\n", reg);
579 if (log) fprintf(stderr, "swap top of stack\n");
588 if (log) fprintf(stderr, "rotate top three of stack\n");
592 // pop stack, dereference, push result
594 *sp = *((uint64_t*)value);
595 if (log) fprintf(stderr, "x-dereference 0x%llX\n", (uint64_t)value);
602 if (log) fprintf(stderr, "abs\n");
608 if (log) fprintf(stderr, "and\n");
614 if (log) fprintf(stderr, "div\n");
620 if (log) fprintf(stderr, "minus\n");
626 if (log) fprintf(stderr, "module\n");
632 if (log) fprintf(stderr, "mul\n");
637 if (log) fprintf(stderr, "neg\n");
643 if (log) fprintf(stderr, "not\n");
649 if (log) fprintf(stderr, "or\n");
655 if (log) fprintf(stderr, "plus\n");
658 case DW_OP_plus_uconst:
659 // pop stack, add uelb128 constant, push result
660 *sp += addressSpace.getULEB128(p, expressionEnd);
661 if (log) fprintf(stderr, "add constant\n");
667 if (log) fprintf(stderr, "shift left\n");
673 if (log) fprintf(stderr, "shift left\n");
679 *sp = svalue >> value;
680 if (log) fprintf(stderr, "shift left arithmetric\n");
686 if (log) fprintf(stderr, "xor\n");
690 svalue = (int16_t)addressSpace.get16(p);
693 if (log) fprintf(stderr, "skip %lld\n", (uint64_t)svalue);
697 svalue = (int16_t)addressSpace.get16(p);
701 if (log) fprintf(stderr, "bra %lld\n", (uint64_t)svalue);
706 *sp = (*sp == value);
707 if (log) fprintf(stderr, "eq\n");
712 *sp = (*sp >= value);
713 if (log) fprintf(stderr, "ge\n");
719 if (log) fprintf(stderr, "gt\n");
724 *sp = (*sp <= value);
725 if (log) fprintf(stderr, "le\n");
731 if (log) fprintf(stderr, "lt\n");
736 *sp = (*sp != value);
737 if (log) fprintf(stderr, "ne\n");
772 value = opcode - DW_OP_lit0;
774 if (log) fprintf(stderr, "push literal 0x%llX\n", (uint64_t)value);
809 reg = opcode - DW_OP_reg0;
810 *(++sp) = registers.getRegister(reg);
811 if (log) fprintf(stderr, "push reg %d\n", reg);
815 reg = addressSpace.getULEB128(p, expressionEnd);
816 *(++sp) = registers.getRegister(reg);
817 if (log) fprintf(stderr, "push reg %d + 0x%llX\n", reg, (uint64_t)svalue);
852 reg = opcode - DW_OP_breg0;
853 svalue = addressSpace.getSLEB128(p, expressionEnd);
854 *(++sp) = registers.getRegister(reg) + svalue;
855 if (log) fprintf(stderr, "push reg %d + 0x%llX\n", reg, (uint64_t)svalue);
859 reg = addressSpace.getULEB128(p, expressionEnd);
860 svalue = addressSpace.getSLEB128(p, expressionEnd);
861 *(++sp) = registers.getRegister(reg) + svalue;
862 if (log) fprintf(stderr, "push reg %d + 0x%llX\n", reg, (uint64_t)svalue);
866 ABORT("DW_OP_fbreg not implemented");
870 ABORT("DW_OP_piece not implemented");
873 case DW_OP_deref_size:
874 // pop stack, dereference, push result
876 switch ( addressSpace.get8(p++) ) {
878 value = addressSpace.get8(value);
881 value = addressSpace.get16(value);
884 value = addressSpace.get32(value);
887 value = addressSpace.get64(value);
890 ABORT("DW_OP_deref_size with bad size");
893 if (log) fprintf(stderr, "sized dereference 0x%llX\n", (uint64_t)value);
896 case DW_OP_xderef_size:
898 case DW_OP_push_object_addres:
903 ABORT("dwarf opcode not implemented");
907 if (log) fprintf(stderr, "expression evaluates to 0x%llX\n", (uint64_t)*sp);
914 // x86_64 specific functions
917 template <typename A, typename R>
918 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_x86_64&)
920 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)DW_X86_64_RET_ADDR );
921 return DW_X86_64_RET_ADDR;
924 template <typename A, typename R>
925 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_x86_64&)
927 return (regNum == DW_X86_64_RET_ADDR);
930 template <typename A, typename R>
931 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
932 const Registers_x86_64& registers)
934 if ( prolog.cfaRegister != 0 )
935 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
936 else if ( prolog.cfaExpression != 0 )
937 return evaluateExpression(prolog.cfaExpression, addressSpace, registers, 0);
939 ABORT("getCFA(): unknown location for x86_64 cfa");
944 template <typename A, typename R>
945 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_x86_64&)
947 return UNWIND_X86_64_MODE_DWARF;
950 template <typename A, typename R>
951 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_x86&)
953 return UNWIND_X86_MODE_DWARF;
958 template <typename A, typename R>
959 uint32_t DwarfInstructions<A,R>::getRBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure)
961 if ( (regOffsetFromBaseOffset < 0) || (regOffsetFromBaseOffset > 32) ) {
965 unsigned int slotIndex = regOffsetFromBaseOffset/8;
969 return UNWIND_X86_64_REG_RBX << (slotIndex*3);
971 return UNWIND_X86_64_REG_R12 << (slotIndex*3);
973 return UNWIND_X86_64_REG_R13 << (slotIndex*3);
975 return UNWIND_X86_64_REG_R14 << (slotIndex*3);
977 return UNWIND_X86_64_REG_R15 << (slotIndex*3);
987 template <typename A, typename R>
988 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
989 const Registers_x86_64& r, const typename CFI_Parser<A>::PrologInfo& prolog,
990 char warningBuffer[1024])
992 warningBuffer[0] = '\0';
994 if ( prolog.registerSavedTwiceInCIE == DW_X86_64_RET_ADDR ) {
995 warningBuffer[0] = '\0'; // silently disable conversion to compact unwind by linker
996 return UNWIND_X86_64_MODE_DWARF;
998 // don't create compact unwind info for unsupported dwarf kinds
999 if ( prolog.registerSavedMoreThanOnce ) {
1000 strcpy(warningBuffer, "register saved more than once (might be shrink wrap)");
1001 return UNWIND_X86_64_MODE_DWARF;
1003 if ( prolog.cfaOffsetWasNegative ) {
1004 strcpy(warningBuffer, "cfa had negative offset (dwarf might contain epilog)");
1005 return UNWIND_X86_64_MODE_DWARF;
1007 if ( prolog.spExtraArgSize != 0 ) {
1008 strcpy(warningBuffer, "dwarf uses DW_CFA_GNU_args_size");
1009 return UNWIND_X86_64_MODE_DWARF;
1011 if ( prolog.sameValueUsed ) {
1012 strcpy(warningBuffer, "dwarf uses DW_CFA_same_value");
1013 return UNWIND_X86_64_MODE_DWARF;
1016 // figure out which kind of frame this function uses
1017 bool standardRBPframe = (
1018 (prolog.cfaRegister == UNW_X86_64_RBP)
1019 && (prolog.cfaRegisterOffset == 16)
1020 && (prolog.savedRegisters[UNW_X86_64_RBP].location == CFI_Parser<A>::kRegisterInCFA)
1021 && (prolog.savedRegisters[UNW_X86_64_RBP].value == -16) );
1022 bool standardRSPframe = (prolog.cfaRegister == UNW_X86_64_RSP);
1023 if ( !standardRBPframe && !standardRSPframe ) {
1024 // no compact encoding for this
1025 strcpy(warningBuffer, "does not use RBP or RSP based frame");
1026 return UNWIND_X86_64_MODE_DWARF;
1029 // scan which registers are saved
1030 int saveRegisterCount = 0;
1031 bool rbxSaved = false;
1032 bool r12Saved = false;
1033 bool r13Saved = false;
1034 bool r14Saved = false;
1035 bool r15Saved = false;
1036 bool rbpSaved = false;
1037 for (int i=0; i < 64; ++i) {
1038 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1039 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterInCFA ) {
1040 sprintf(warningBuffer, "register %d saved somewhere other that in frame", i);
1041 return UNWIND_X86_64_MODE_DWARF;
1044 case UNW_X86_64_RBX:
1046 ++saveRegisterCount;
1048 case UNW_X86_64_R12:
1050 ++saveRegisterCount;
1052 case UNW_X86_64_R13:
1054 ++saveRegisterCount;
1056 case UNW_X86_64_R14:
1058 ++saveRegisterCount;
1060 case UNW_X86_64_R15:
1062 ++saveRegisterCount;
1064 case UNW_X86_64_RBP:
1066 ++saveRegisterCount;
1068 case DW_X86_64_RET_ADDR:
1071 sprintf(warningBuffer, "non-standard register %d being saved in prolog", i);
1072 return UNWIND_X86_64_MODE_DWARF;
1076 const int64_t cfaOffsetRBX = prolog.savedRegisters[UNW_X86_64_RBX].value;
1077 const int64_t cfaOffsetR12 = prolog.savedRegisters[UNW_X86_64_R12].value;
1078 const int64_t cfaOffsetR13 = prolog.savedRegisters[UNW_X86_64_R13].value;
1079 const int64_t cfaOffsetR14 = prolog.savedRegisters[UNW_X86_64_R14].value;
1080 const int64_t cfaOffsetR15 = prolog.savedRegisters[UNW_X86_64_R15].value;
1081 const int64_t cfaOffsetRBP = prolog.savedRegisters[UNW_X86_64_RBP].value;
1083 // encode standard RBP frames
1084 compact_unwind_encoding_t encoding = 0;
1085 if ( standardRBPframe ) {
1087 // +--------------+ <- CFA
1091 // +--------------+ <- rbp
1095 // +--------------+ <- CFA - offset+16
1097 // +--------------+ <- CFA - offset+8
1099 // +--------------+ <- CFA - offset
1105 encoding = UNWIND_X86_64_MODE_RBP_FRAME;
1107 // find save location of farthest register from rbp
1108 int furthestCfaOffset = 0;
1109 if ( rbxSaved & (cfaOffsetRBX < furthestCfaOffset) )
1110 furthestCfaOffset = cfaOffsetRBX;
1111 if ( r12Saved & (cfaOffsetR12 < furthestCfaOffset) )
1112 furthestCfaOffset = cfaOffsetR12;
1113 if ( r13Saved & (cfaOffsetR13 < furthestCfaOffset) )
1114 furthestCfaOffset = cfaOffsetR13;
1115 if ( r14Saved & (cfaOffsetR14 < furthestCfaOffset) )
1116 furthestCfaOffset = cfaOffsetR14;
1117 if ( r15Saved & (cfaOffsetR15 < furthestCfaOffset) )
1118 furthestCfaOffset = cfaOffsetR15;
1120 if ( furthestCfaOffset == 0 ) {
1121 // no registers saved, nothing more to encode
1125 // add stack offset to encoding
1126 int rbpOffset = furthestCfaOffset + 16;
1127 int encodedOffset = rbpOffset/(-8);
1128 if ( encodedOffset > 255 ) {
1129 strcpy(warningBuffer, "offset of saved registers too far to encode");
1130 return UNWIND_X86_64_MODE_DWARF;
1132 encoding |= (encodedOffset << __builtin_ctz(UNWIND_X86_64_RBP_FRAME_OFFSET));
1134 // add register saved from each stack location
1135 bool encodingFailure = false;
1137 encoding |= getRBPEncodedRegister(UNW_X86_64_RBX, cfaOffsetRBX - furthestCfaOffset, encodingFailure);
1139 encoding |= getRBPEncodedRegister(UNW_X86_64_R12, cfaOffsetR12 - furthestCfaOffset, encodingFailure);
1141 encoding |= getRBPEncodedRegister(UNW_X86_64_R13, cfaOffsetR13 - furthestCfaOffset, encodingFailure);
1143 encoding |= getRBPEncodedRegister(UNW_X86_64_R14, cfaOffsetR14 - furthestCfaOffset, encodingFailure);
1145 encoding |= getRBPEncodedRegister(UNW_X86_64_R15, cfaOffsetR15 - furthestCfaOffset, encodingFailure);
1147 if ( encodingFailure ){
1148 strcpy(warningBuffer, "saved registers not contiguous");
1149 return UNWIND_X86_64_MODE_DWARF;
1156 // +--------------+ <- CFA
1160 // +--------------+ <- CFA - 16
1162 // +--------------+ <- CFA - 24
1164 // +--------------+ <- CFA - 32
1166 // +--------------+ <- CFA - 40
1168 // +--------------+ <- CFA - 48
1170 // +--------------+ <- CFA - 56
1175 // for RSP based frames we need to encode stack size in unwind info
1176 encoding = UNWIND_X86_64_MODE_STACK_IMMD;
1177 uint64_t stackValue = prolog.cfaRegisterOffset / 8;
1178 uint32_t stackAdjust = 0;
1179 bool immedStackSize = true;
1180 const uint32_t stackMaxImmedValue = EXTRACT_BITS(0xFFFFFFFF,UNWIND_X86_64_FRAMELESS_STACK_SIZE);
1181 if ( stackValue > stackMaxImmedValue ) {
1182 // stack size is too big to fit as an immediate value, so encode offset of subq instruction in function
1183 if ( prolog.codeOffsetAtStackDecrement == 0 ) {
1184 strcpy(warningBuffer, "stack size is large but stack subq instruction not found");
1185 return UNWIND_X86_64_MODE_DWARF;
1187 pint_t functionContentAdjustStackIns = funcAddr + prolog.codeOffsetAtStackDecrement - 4;
1189 uint32_t stackDecrementInCode = addressSpace.get32(functionContentAdjustStackIns);
1190 stackAdjust = (prolog.cfaRegisterOffset - stackDecrementInCode)/8;
1193 strcpy(warningBuffer, "stack size is large but stack subq instruction not found");
1194 return UNWIND_X86_64_MODE_DWARF;
1196 stackValue = functionContentAdjustStackIns - funcAddr;
1197 immedStackSize = false;
1198 if ( stackAdjust > 7 ) {
1199 strcpy(warningBuffer, "stack subq instruction is too different from dwarf stack size");
1200 return UNWIND_X86_64_MODE_DWARF;
1202 encoding = UNWIND_X86_64_MODE_STACK_IND;
1206 // validate that saved registers are all within 6 slots abutting return address
1208 for (int i=0; i < 6;++i)
1211 if ( cfaOffsetR15 < -56 ) {
1212 strcpy(warningBuffer, "r15 is saved too far from return address");
1213 return UNWIND_X86_64_MODE_DWARF;
1215 registers[(cfaOffsetR15+56)/8] = UNWIND_X86_64_REG_R15;
1218 if ( cfaOffsetR14 < -56 ) {
1219 strcpy(warningBuffer, "r14 is saved too far from return address");
1220 return UNWIND_X86_64_MODE_DWARF;
1222 registers[(cfaOffsetR14+56)/8] = UNWIND_X86_64_REG_R14;
1225 if ( cfaOffsetR13 < -56 ) {
1226 strcpy(warningBuffer, "r13 is saved too far from return address");
1227 return UNWIND_X86_64_MODE_DWARF;
1229 registers[(cfaOffsetR13+56)/8] = UNWIND_X86_64_REG_R13;
1232 if ( cfaOffsetR12 < -56 ) {
1233 strcpy(warningBuffer, "r12 is saved too far from return address");
1234 return UNWIND_X86_64_MODE_DWARF;
1236 registers[(cfaOffsetR12+56)/8] = UNWIND_X86_64_REG_R12;
1239 if ( cfaOffsetRBX < -56 ) {
1240 strcpy(warningBuffer, "rbx is saved too far from return address");
1241 return UNWIND_X86_64_MODE_DWARF;
1243 registers[(cfaOffsetRBX+56)/8] = UNWIND_X86_64_REG_RBX;
1246 if ( cfaOffsetRBP < -56 ) {
1247 strcpy(warningBuffer, "rbp is saved too far from return address");
1248 return UNWIND_X86_64_MODE_DWARF;
1250 registers[(cfaOffsetRBP+56)/8] = UNWIND_X86_64_REG_RBP;
1253 // validate that saved registers are contiguous and abut return address on stack
1254 for (int i=0; i < saveRegisterCount; ++i) {
1255 if ( registers[5-i] == 0 ) {
1256 strcpy(warningBuffer, "registers not save contiguously in stack");
1257 return UNWIND_X86_64_MODE_DWARF;
1261 // encode register permutation
1262 // the 10-bits are encoded differently depending on the number of registers saved
1264 for (int i=6-saveRegisterCount; i < 6; ++i) {
1266 for (int j=6-saveRegisterCount; j < i; ++j) {
1267 if ( registers[j] < registers[i] )
1270 renumregs[i] = registers[i] - countless -1;
1272 uint32_t permutationEncoding = 0;
1273 switch ( saveRegisterCount ) {
1275 permutationEncoding |= (120*renumregs[0] + 24*renumregs[1] + 6*renumregs[2] + 2*renumregs[3] + renumregs[4]);
1278 permutationEncoding |= (120*renumregs[1] + 24*renumregs[2] + 6*renumregs[3] + 2*renumregs[4] + renumregs[5]);
1281 permutationEncoding |= (60*renumregs[2] + 12*renumregs[3] + 3*renumregs[4] + renumregs[5]);
1284 permutationEncoding |= (20*renumregs[3] + 4*renumregs[4] + renumregs[5]);
1287 permutationEncoding |= (5*renumregs[4] + renumregs[5]);
1290 permutationEncoding |= (renumregs[5]);
1294 encoding |= (stackValue << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_SIZE));
1295 encoding |= (stackAdjust << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_ADJUST));
1296 encoding |= (saveRegisterCount << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT));
1297 encoding |= (permutationEncoding << __builtin_ctz(UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION));
1306 // x86 specific functions
1308 template <typename A, typename R>
1309 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_x86&)
1311 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)DW_X86_RET_ADDR );
1312 return DW_X86_RET_ADDR;
1315 template <typename A, typename R>
1316 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_x86&)
1318 return (regNum == DW_X86_RET_ADDR);
1321 template <typename A, typename R>
1322 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
1323 const Registers_x86& registers)
1325 if ( prolog.cfaRegister != 0 )
1326 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
1327 else if ( prolog.cfaExpression != 0 )
1328 return evaluateExpression(prolog.cfaExpression, addressSpace, registers, 0);
1330 ABORT("getCFA(): unknown location for x86 cfa");
1337 template <typename A, typename R>
1338 uint32_t DwarfInstructions<A,R>::getEBPEncodedRegister(uint32_t reg, int32_t regOffsetFromBaseOffset, bool& failure)
1340 if ( (regOffsetFromBaseOffset < 0) || (regOffsetFromBaseOffset > 16) ) {
1344 unsigned int slotIndex = regOffsetFromBaseOffset/4;
1348 return UNWIND_X86_REG_EBX << (slotIndex*3);
1350 return UNWIND_X86_REG_ECX << (slotIndex*3);
1352 return UNWIND_X86_REG_EDX << (slotIndex*3);
1354 return UNWIND_X86_REG_EDI << (slotIndex*3);
1356 return UNWIND_X86_REG_ESI << (slotIndex*3);
1364 template <typename A, typename R>
1365 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
1366 const Registers_x86& r, const typename CFI_Parser<A>::PrologInfo& prolog,
1367 char warningBuffer[1024])
1369 warningBuffer[0] = '\0';
1371 if ( prolog.registerSavedTwiceInCIE == DW_X86_RET_ADDR ) {
1372 warningBuffer[0] = '\0'; // silently disable conversion to compact unwind by linker
1373 return UNWIND_X86_64_MODE_DWARF;
1375 // don't create compact unwind info for unsupported dwarf kinds
1376 if ( prolog.registerSavedMoreThanOnce ) {
1377 strcpy(warningBuffer, "register saved more than once (might be shrink wrap)");
1378 return UNWIND_X86_MODE_DWARF;
1380 if ( prolog.spExtraArgSize != 0 ) {
1381 strcpy(warningBuffer, "dwarf uses DW_CFA_GNU_args_size");
1382 return UNWIND_X86_MODE_DWARF;
1384 if ( prolog.sameValueUsed ) {
1385 strcpy(warningBuffer, "dwarf uses DW_CFA_same_value");
1386 return UNWIND_X86_MODE_DWARF;
1389 // figure out which kind of frame this function uses
1390 bool standardEBPframe = (
1391 (prolog.cfaRegister == UNW_X86_EBP)
1392 && (prolog.cfaRegisterOffset == 8)
1393 && (prolog.savedRegisters[UNW_X86_EBP].location == CFI_Parser<A>::kRegisterInCFA)
1394 && (prolog.savedRegisters[UNW_X86_EBP].value == -8) );
1395 bool standardESPframe = (prolog.cfaRegister == UNW_X86_ESP);
1396 if ( !standardEBPframe && !standardESPframe ) {
1397 // no compact encoding for this
1398 strcpy(warningBuffer, "does not use EBP or ESP based frame");
1399 return UNWIND_X86_MODE_DWARF;
1402 // scan which registers are saved
1403 int saveRegisterCount = 0;
1404 bool ebxSaved = false;
1405 bool ecxSaved = false;
1406 bool edxSaved = false;
1407 bool esiSaved = false;
1408 bool ediSaved = false;
1409 bool ebpSaved = false;
1410 for (int i=0; i < 64; ++i) {
1411 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterUnused ) {
1412 if ( prolog.savedRegisters[i].location != CFI_Parser<A>::kRegisterInCFA ) {
1413 sprintf(warningBuffer, "register %d saved somewhere other that in frame", i);
1414 return UNWIND_X86_MODE_DWARF;
1419 ++saveRegisterCount;
1423 ++saveRegisterCount;
1427 ++saveRegisterCount;
1431 ++saveRegisterCount;
1435 ++saveRegisterCount;
1439 ++saveRegisterCount;
1441 case DW_X86_RET_ADDR:
1444 sprintf(warningBuffer, "non-standard register %d being saved in prolog", i);
1445 return UNWIND_X86_MODE_DWARF;
1449 const int32_t cfaOffsetEBX = prolog.savedRegisters[UNW_X86_EBX].value;
1450 const int32_t cfaOffsetECX = prolog.savedRegisters[UNW_X86_ECX].value;
1451 const int32_t cfaOffsetEDX = prolog.savedRegisters[UNW_X86_EDX].value;
1452 const int32_t cfaOffsetEDI = prolog.savedRegisters[UNW_X86_EDI].value;
1453 const int32_t cfaOffsetESI = prolog.savedRegisters[UNW_X86_ESI].value;
1454 const int32_t cfaOffsetEBP = prolog.savedRegisters[UNW_X86_EBP].value;
1456 // encode standard RBP frames
1457 compact_unwind_encoding_t encoding = 0;
1458 if ( standardEBPframe ) {
1460 // +--------------+ <- CFA
1464 // +--------------+ <- ebp
1468 // +--------------+ <- CFA - offset+8
1470 // +--------------+ <- CFA - offset+e
1472 // +--------------+ <- CFA - offset
1478 encoding = UNWIND_X86_MODE_EBP_FRAME;
1480 // find save location of farthest register from ebp
1481 int furthestCfaOffset = 0;
1482 if ( ebxSaved & (cfaOffsetEBX < furthestCfaOffset) )
1483 furthestCfaOffset = cfaOffsetEBX;
1484 if ( ecxSaved & (cfaOffsetECX < furthestCfaOffset) )
1485 furthestCfaOffset = cfaOffsetECX;
1486 if ( edxSaved & (cfaOffsetEDX < furthestCfaOffset) )
1487 furthestCfaOffset = cfaOffsetEDX;
1488 if ( ediSaved & (cfaOffsetEDI < furthestCfaOffset) )
1489 furthestCfaOffset = cfaOffsetEDI;
1490 if ( esiSaved & (cfaOffsetESI < furthestCfaOffset) )
1491 furthestCfaOffset = cfaOffsetESI;
1493 if ( furthestCfaOffset == 0 ) {
1494 // no registers saved, nothing more to encode
1498 // add stack offset to encoding
1499 int ebpOffset = furthestCfaOffset + 8;
1500 int encodedOffset = ebpOffset/(-4);
1501 if ( encodedOffset > 255 ) {
1502 strcpy(warningBuffer, "offset of saved registers too far to encode");
1503 return UNWIND_X86_MODE_DWARF;
1505 encoding |= (encodedOffset << __builtin_ctz(UNWIND_X86_EBP_FRAME_OFFSET));
1507 // add register saved from each stack location
1508 bool encodingFailure = false;
1510 encoding |= getEBPEncodedRegister(UNW_X86_EBX, cfaOffsetEBX - furthestCfaOffset, encodingFailure);
1512 encoding |= getEBPEncodedRegister(UNW_X86_ECX, cfaOffsetECX - furthestCfaOffset, encodingFailure);
1514 encoding |= getEBPEncodedRegister(UNW_X86_EDX, cfaOffsetEDX - furthestCfaOffset, encodingFailure);
1516 encoding |= getEBPEncodedRegister(UNW_X86_EDI, cfaOffsetEDI - furthestCfaOffset, encodingFailure);
1518 encoding |= getEBPEncodedRegister(UNW_X86_ESI, cfaOffsetESI - furthestCfaOffset, encodingFailure);
1520 if ( encodingFailure ){
1521 strcpy(warningBuffer, "saved registers not contiguous");
1522 return UNWIND_X86_MODE_DWARF;
1529 // +--------------+ <- CFA
1533 // +--------------+ <- CFA - 8
1535 // +--------------+ <- CFA - 12
1537 // +--------------+ <- CFA - 16
1539 // +--------------+ <- CFA - 20
1541 // +--------------+ <- CFA - 24
1543 // +--------------+ <- CFA - 28
1548 // for ESP based frames we need to encode stack size in unwind info
1549 encoding = UNWIND_X86_MODE_STACK_IMMD;
1550 uint64_t stackValue = prolog.cfaRegisterOffset / 4;
1551 uint32_t stackAdjust = 0;
1552 bool immedStackSize = true;
1553 const uint32_t stackMaxImmedValue = EXTRACT_BITS(0xFFFFFFFF,UNWIND_X86_FRAMELESS_STACK_SIZE);
1554 if ( stackValue > stackMaxImmedValue ) {
1555 // stack size is too big to fit as an immediate value, so encode offset of subq instruction in function
1556 pint_t functionContentAdjustStackIns = funcAddr + prolog.codeOffsetAtStackDecrement - 4;
1557 uint32_t stackDecrementInCode = addressSpace.get32(functionContentAdjustStackIns);
1558 stackAdjust = (prolog.cfaRegisterOffset - stackDecrementInCode)/4;
1559 stackValue = functionContentAdjustStackIns - funcAddr;
1560 immedStackSize = false;
1561 if ( stackAdjust > 7 ) {
1562 strcpy(warningBuffer, "stack subq instruction is too different from dwarf stack size");
1563 return UNWIND_X86_MODE_DWARF;
1565 encoding = UNWIND_X86_MODE_STACK_IND;
1569 // validate that saved registers are all within 6 slots abutting return address
1571 for (int i=0; i < 6;++i)
1574 if ( cfaOffsetEBX < -28 ) {
1575 strcpy(warningBuffer, "ebx is saved too far from return address");
1576 return UNWIND_X86_MODE_DWARF;
1578 registers[(cfaOffsetEBX+28)/4] = UNWIND_X86_REG_EBX;
1581 if ( cfaOffsetECX < -28 ) {
1582 strcpy(warningBuffer, "ecx is saved too far from return address");
1583 return UNWIND_X86_MODE_DWARF;
1585 registers[(cfaOffsetECX+28)/4] = UNWIND_X86_REG_ECX;
1588 if ( cfaOffsetEDX < -28 ) {
1589 strcpy(warningBuffer, "edx is saved too far from return address");
1590 return UNWIND_X86_MODE_DWARF;
1592 registers[(cfaOffsetEDX+28)/4] = UNWIND_X86_REG_EDX;
1595 if ( cfaOffsetEDI < -28 ) {
1596 strcpy(warningBuffer, "edi is saved too far from return address");
1597 return UNWIND_X86_MODE_DWARF;
1599 registers[(cfaOffsetEDI+28)/4] = UNWIND_X86_REG_EDI;
1602 if ( cfaOffsetESI < -28 ) {
1603 strcpy(warningBuffer, "esi is saved too far from return address");
1604 return UNWIND_X86_MODE_DWARF;
1606 registers[(cfaOffsetESI+28)/4] = UNWIND_X86_REG_ESI;
1609 if ( cfaOffsetEBP < -28 ) {
1610 strcpy(warningBuffer, "ebp is saved too far from return address");
1611 return UNWIND_X86_MODE_DWARF;
1613 registers[(cfaOffsetEBP+28)/4] = UNWIND_X86_REG_EBP;
1616 // validate that saved registers are contiguous and abut return address on stack
1617 for (int i=0; i < saveRegisterCount; ++i) {
1618 if ( registers[5-i] == 0 ) {
1619 strcpy(warningBuffer, "registers not save contiguously in stack");
1620 return UNWIND_X86_MODE_DWARF;
1624 // encode register permutation
1625 // the 10-bits are encoded differently depending on the number of registers saved
1627 for (int i=6-saveRegisterCount; i < 6; ++i) {
1629 for (int j=6-saveRegisterCount; j < i; ++j) {
1630 if ( registers[j] < registers[i] )
1633 renumregs[i] = registers[i] - countless -1;
1635 uint32_t permutationEncoding = 0;
1636 switch ( saveRegisterCount ) {
1638 permutationEncoding |= (120*renumregs[0] + 24*renumregs[1] + 6*renumregs[2] + 2*renumregs[3] + renumregs[4]);
1641 permutationEncoding |= (120*renumregs[1] + 24*renumregs[2] + 6*renumregs[3] + 2*renumregs[4] + renumregs[5]);
1644 permutationEncoding |= (60*renumregs[2] + 12*renumregs[3] + 3*renumregs[4] + renumregs[5]);
1647 permutationEncoding |= (20*renumregs[3] + 4*renumregs[4] + renumregs[5]);
1650 permutationEncoding |= (5*renumregs[4] + renumregs[5]);
1653 permutationEncoding |= (renumregs[5]);
1657 encoding |= (stackValue << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_SIZE));
1658 encoding |= (stackAdjust << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_ADJUST));
1659 encoding |= (saveRegisterCount << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_REG_COUNT));
1660 encoding |= (permutationEncoding << __builtin_ctz(UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION));
1672 // ppc specific functions
1674 template <typename A, typename R>
1675 int DwarfInstructions<A,R>::lastRestoreReg(const Registers_ppc&)
1677 COMPILE_TIME_ASSERT( (int)CFI_Parser<A>::kMaxRegisterNumber > (int)UNW_PPC_SPEFSCR );
1678 return UNW_PPC_SPEFSCR;
1681 template <typename A, typename R>
1682 bool DwarfInstructions<A,R>::isReturnAddressRegister(int regNum, const Registers_ppc&)
1684 return (regNum == UNW_PPC_LR);
1687 template <typename A, typename R>
1688 typename A::pint_t DwarfInstructions<A,R>::getCFA(A& addressSpace, const typename CFI_Parser<A>::PrologInfo& prolog,
1689 const Registers_ppc& registers)
1691 if ( prolog.cfaRegister != 0 )
1692 return registers.getRegister(prolog.cfaRegister) + prolog.cfaRegisterOffset;
1693 else if ( prolog.cfaExpression != 0 )
1694 return evaluateExpression(prolog.cfaExpression, addressSpace, registers, 0);
1696 ABORT("getCFA(): unknown location for ppc cfa");
1700 template <typename A, typename R>
1701 compact_unwind_encoding_t DwarfInstructions<A,R>::encodeToUseDwarf(const Registers_ppc&)
1703 return UNWIND_X86_MODE_DWARF;
1707 template <typename A, typename R>
1708 compact_unwind_encoding_t DwarfInstructions<A,R>::createCompactEncodingFromProlog(A& addressSpace, pint_t funcAddr,
1709 const Registers_ppc& r, const typename CFI_Parser<A>::PrologInfo& prolog,
1710 char warningBuffer[1024])
1712 warningBuffer[0] = '\0';
1713 return UNWIND_X86_MODE_DWARF;
1719 } // namespace libunwind
1722 #endif // __DWARF_INSTRUCTIONS_HPP__