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