Subversion Repositories Kolibri OS

Rev

Rev 6515 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* DWARF2 exception handling and frame unwind runtime interface routines.
  2.    Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GCC.
  5.  
  6.    GCC is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 3, or (at your option)
  9.    any later version.
  10.  
  11.    GCC is distributed in the hope that it will be useful, but WITHOUT
  12.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  13.    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  14.    License for more details.
  15.  
  16.    Under Section 7 of GPL version 3, you are granted additional
  17.    permissions described in the GCC Runtime Library Exception, version
  18.    3.1, as published by the Free Software Foundation.
  19.  
  20.    You should have received a copy of the GNU General Public License and
  21.    a copy of the GCC Runtime Library Exception along with this program;
  22.    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
  23.    <http://www.gnu.org/licenses/>.  */
  24.  
  25. #include "tconfig.h"
  26. #include "tsystem.h"
  27. #include "coretypes.h"
  28. #include "tm.h"
  29. #include "libgcc_tm.h"
  30. #include "dwarf2.h"
  31. #include "unwind.h"
  32. #ifdef __USING_SJLJ_EXCEPTIONS__
  33. # define NO_SIZE_OF_ENCODED_VALUE
  34. #endif
  35. #include "unwind-pe.h"
  36. #include "unwind-dw2-fde.h"
  37. #include "gthr.h"
  38. #include "unwind-dw2.h"
  39.  
  40. #ifdef HAVE_SYS_SDT_H
  41. #include <sys/sdt.h>
  42. #endif
  43.  
  44. #ifndef __USING_SJLJ_EXCEPTIONS__
  45.  
  46. #ifndef __LIBGCC_STACK_GROWS_DOWNWARD__
  47. #define __LIBGCC_STACK_GROWS_DOWNWARD__ 0
  48. #else
  49. #undef __LIBGCC_STACK_GROWS_DOWNWARD__
  50. #define __LIBGCC_STACK_GROWS_DOWNWARD__ 1
  51. #endif
  52.  
  53. /* Dwarf frame registers used for pre gcc 3.0 compiled glibc.  */
  54. #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
  55. #define PRE_GCC3_DWARF_FRAME_REGISTERS __LIBGCC_DWARF_FRAME_REGISTERS__
  56. #endif
  57.  
  58. /* ??? For the public function interfaces, we tend to gcc_assert that the
  59.    column numbers are in range.  For the dwarf2 unwind info this does happen,
  60.    although so far in a case that doesn't actually matter.
  61.  
  62.    See PR49146, in which a call from x86_64 ms abi to x86_64 unix abi stores
  63.    the call-saved xmm registers and annotates them.  We havn't bothered
  64.    providing support for the xmm registers for the x86_64 port primarily
  65.    because the 64-bit windows targets don't use dwarf2 unwind, using sjlj or
  66.    SEH instead.  Adding the support for unix targets would generally be a
  67.    waste.  However, some runtime libraries supplied with ICC do contain such
  68.    an unorthodox transition, as well as the unwind info to match.  This loss
  69.    of register restoration doesn't matter in practice, because the exception
  70.    is caught in the native unix abi, where all of the xmm registers are
  71.    call clobbered.
  72.  
  73.    Ideally, we'd record some bit to notice when we're failing to restore some
  74.    register recorded in the unwind info, but to do that we need annotation on
  75.    the unix->ms abi edge, so that we know when the register data may be
  76.    discarded.  And since this edge is also within the ICC library, we're
  77.    unlikely to be able to get the new annotation.
  78.  
  79.    Barring a magic solution to restore the ms abi defined 128-bit xmm registers
  80.    (as distictly opposed to the full runtime width) without causing extra
  81.    overhead for normal unix abis, the best solution seems to be to simply
  82.    ignore unwind data for unknown columns.  */
  83.  
  84. #define UNWIND_COLUMN_IN_RANGE(x) \
  85.     __builtin_expect((x) <= __LIBGCC_DWARF_FRAME_REGISTERS__, 1)
  86.  
  87. #ifdef REG_VALUE_IN_UNWIND_CONTEXT
  88. typedef _Unwind_Word _Unwind_Context_Reg_Val;
  89.  
  90. #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
  91. #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
  92. #endif
  93.  
  94. static inline _Unwind_Word
  95. _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
  96. {
  97.   return val;
  98. }
  99.  
  100. static inline _Unwind_Context_Reg_Val
  101. _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
  102. {
  103.   return val;
  104. }
  105. #else
  106. typedef void *_Unwind_Context_Reg_Val;
  107.  
  108. static inline _Unwind_Word
  109. _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
  110. {
  111.   return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
  112. }
  113.  
  114. static inline _Unwind_Context_Reg_Val
  115. _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
  116. {
  117.   return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
  118. }
  119. #endif
  120.  
  121. #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
  122. #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
  123. #endif
  124.  
  125. /* This is the register and unwind state for a particular frame.  This
  126.    provides the information necessary to unwind up past a frame and return
  127.    to its caller.  */
  128. struct _Unwind_Context
  129. {
  130.   _Unwind_Context_Reg_Val reg[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
  131.   void *cfa;
  132.   void *ra;
  133.   void *lsda;
  134.   struct dwarf_eh_bases bases;
  135.   /* Signal frame context.  */
  136. #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
  137.   /* Context which has version/args_size/by_value fields.  */
  138. #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
  139.   _Unwind_Word flags;
  140.   /* 0 for now, can be increased when further fields are added to
  141.      struct _Unwind_Context.  */
  142.   _Unwind_Word version;
  143.   _Unwind_Word args_size;
  144.   char by_value[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
  145. };
  146.  
  147. /* Byte size of every register managed by these routines.  */
  148. static unsigned char dwarf_reg_size_table[__LIBGCC_DWARF_FRAME_REGISTERS__+1];
  149.  
  150. /* Read unaligned data from the instruction buffer.  */
  151.  
  152. union unaligned
  153. {
  154.   void *p;
  155.   unsigned u2 __attribute__ ((mode (HI)));
  156.   unsigned u4 __attribute__ ((mode (SI)));
  157.   unsigned u8 __attribute__ ((mode (DI)));
  158.   signed s2 __attribute__ ((mode (HI)));
  159.   signed s4 __attribute__ ((mode (SI)));
  160.   signed s8 __attribute__ ((mode (DI)));
  161. } __attribute__ ((packed));
  162.  
  163. static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
  164. static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
  165.                                                _Unwind_FrameState *);
  166.  
  167. static inline void *
  168. read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
  169.  
  170. static inline int
  171. read_1u (const void *p) { return *(const unsigned char *) p; }
  172.  
  173. static inline int
  174. read_1s (const void *p) { return *(const signed char *) p; }
  175.  
  176. static inline int
  177. read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
  178.  
  179. static inline int
  180. read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
  181.  
  182. static inline unsigned int
  183. read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
  184.  
  185. static inline int
  186. read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
  187.  
  188. static inline unsigned long
  189. read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
  190.  
  191. static inline unsigned long
  192. read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
  193. static inline _Unwind_Word
  194. _Unwind_IsSignalFrame (struct _Unwind_Context *context)
  195. {
  196.   return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
  197. }
  198.  
  199. static inline void
  200. _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
  201. {
  202.   if (val)
  203.     context->flags |= SIGNAL_FRAME_BIT;
  204.   else
  205.     context->flags &= ~SIGNAL_FRAME_BIT;
  206. }
  207.  
  208. static inline _Unwind_Word
  209. _Unwind_IsExtendedContext (struct _Unwind_Context *context)
  210. {
  211.   return (ASSUME_EXTENDED_UNWIND_CONTEXT
  212.           || (context->flags & EXTENDED_CONTEXT_BIT));
  213. }
  214. /* Get the value of register INDEX as saved in CONTEXT.  */
  215.  
  216. inline _Unwind_Word
  217. _Unwind_GetGR (struct _Unwind_Context *context, int index)
  218. {
  219.   int size;
  220.   _Unwind_Context_Reg_Val val;
  221.  
  222. #ifdef DWARF_ZERO_REG
  223.   if (index == DWARF_ZERO_REG)
  224.     return 0;
  225. #endif
  226.  
  227.   index = DWARF_REG_TO_UNWIND_COLUMN (index);
  228.   gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
  229.   size = dwarf_reg_size_table[index];
  230.   val = context->reg[index];
  231.  
  232.   if (_Unwind_IsExtendedContext (context) && context->by_value[index])
  233.     return _Unwind_Get_Unwind_Word (val);
  234.  
  235.   /* This will segfault if the register hasn't been saved.  */
  236.   if (size == sizeof(_Unwind_Ptr))
  237.     return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
  238.   else
  239.     {
  240.       gcc_assert (size == sizeof(_Unwind_Word));
  241.       return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
  242.     }
  243. }
  244.  
  245. static inline void *
  246. _Unwind_GetPtr (struct _Unwind_Context *context, int index)
  247. {
  248.   return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
  249. }
  250.  
  251. /* Get the value of the CFA as saved in CONTEXT.  */
  252.  
  253. _Unwind_Word
  254. _Unwind_GetCFA (struct _Unwind_Context *context)
  255. {
  256.   return (_Unwind_Ptr) context->cfa;
  257. }
  258.  
  259. /* Overwrite the saved value for register INDEX in CONTEXT with VAL.  */
  260.  
  261. inline void
  262. _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
  263. {
  264.   int size;
  265.   void *ptr;
  266.  
  267.   index = DWARF_REG_TO_UNWIND_COLUMN (index);
  268.   gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
  269.   size = dwarf_reg_size_table[index];
  270.  
  271.   if (_Unwind_IsExtendedContext (context) && context->by_value[index])
  272.     {
  273.       context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
  274.       return;
  275.     }
  276.  
  277.   ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
  278.  
  279.   if (size == sizeof(_Unwind_Ptr))
  280.     * (_Unwind_Ptr *) ptr = val;
  281.   else
  282.     {
  283.       gcc_assert (size == sizeof(_Unwind_Word));
  284.       * (_Unwind_Word *) ptr = val;
  285.     }
  286. }
  287.  
  288. /* Get the pointer to a register INDEX as saved in CONTEXT.  */
  289.  
  290. static inline void *
  291. _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
  292. {
  293.   index = DWARF_REG_TO_UNWIND_COLUMN (index);
  294.   if (_Unwind_IsExtendedContext (context) && context->by_value[index])
  295.     return &context->reg[index];
  296.   return (void *) (_Unwind_Internal_Ptr) context->reg[index];
  297. }
  298.  
  299. /* Set the pointer to a register INDEX as saved in CONTEXT.  */
  300.  
  301. static inline void
  302. _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
  303. {
  304.   index = DWARF_REG_TO_UNWIND_COLUMN (index);
  305.   if (_Unwind_IsExtendedContext (context))
  306.     context->by_value[index] = 0;
  307.   context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
  308. }
  309.  
  310. /* Overwrite the saved value for register INDEX in CONTEXT with VAL.  */
  311.  
  312. static inline void
  313. _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
  314.                     _Unwind_Word val)
  315. {
  316.   index = DWARF_REG_TO_UNWIND_COLUMN (index);
  317.   gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
  318.   /* Return column size may be smaller than _Unwind_Context_Reg_Val.  */
  319.   gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val));
  320.  
  321.   context->by_value[index] = 1;
  322.   context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
  323. }
  324.  
  325. /* Return nonzero if register INDEX is stored by value rather than
  326.    by reference.  */
  327.  
  328. static inline int
  329. _Unwind_GRByValue (struct _Unwind_Context *context, int index)
  330. {
  331.   index = DWARF_REG_TO_UNWIND_COLUMN (index);
  332.   return context->by_value[index];
  333. }
  334.  
  335. /* Retrieve the return address for CONTEXT.  */
  336.  
  337. inline _Unwind_Ptr
  338. _Unwind_GetIP (struct _Unwind_Context *context)
  339. {
  340.   return (_Unwind_Ptr) context->ra;
  341. }
  342.  
  343. /* Retrieve the return address and flag whether that IP is before
  344.    or after first not yet fully executed instruction.  */
  345.  
  346. inline _Unwind_Ptr
  347. _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
  348. {
  349.   *ip_before_insn = _Unwind_IsSignalFrame (context);
  350.   return (_Unwind_Ptr) context->ra;
  351. }
  352.  
  353. /* Overwrite the return address for CONTEXT with VAL.  */
  354.  
  355. inline void
  356. _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
  357. {
  358.   context->ra = (void *) val;
  359. }
  360.  
  361. void *
  362. _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
  363. {
  364.   return context->lsda;
  365. }
  366.  
  367. _Unwind_Ptr
  368. _Unwind_GetRegionStart (struct _Unwind_Context *context)
  369. {
  370.   return (_Unwind_Ptr) context->bases.func;
  371. }
  372.  
  373. void *
  374. _Unwind_FindEnclosingFunction (void *pc)
  375. {
  376.   struct dwarf_eh_bases bases;
  377.   const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
  378.   if (fde)
  379.     return bases.func;
  380.   else
  381.     return NULL;
  382. }
  383.  
  384. #ifndef __ia64__
  385. _Unwind_Ptr
  386. _Unwind_GetDataRelBase (struct _Unwind_Context *context)
  387. {
  388.   return (_Unwind_Ptr) context->bases.dbase;
  389. }
  390.  
  391. _Unwind_Ptr
  392. _Unwind_GetTextRelBase (struct _Unwind_Context *context)
  393. {
  394.   return (_Unwind_Ptr) context->bases.tbase;
  395. }
  396. #endif
  397.  
  398. /* Extract any interesting information from the CIE for the translation
  399.    unit F belongs to.  Return a pointer to the byte after the augmentation,
  400.    or NULL if we encountered an undecipherable augmentation.  */
  401.  
  402. static const unsigned char *
  403. extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
  404.                   _Unwind_FrameState *fs)
  405. {
  406.   const unsigned char *aug = cie->augmentation;
  407.   const unsigned char *p = aug + strlen ((const char *)aug) + 1;
  408.   const unsigned char *ret = NULL;
  409.   _uleb128_t utmp;
  410.   _sleb128_t stmp;
  411.  
  412.   /* g++ v2 "eh" has pointer immediately following augmentation string,
  413.      so it must be handled first.  */
  414.   if (aug[0] == 'e' && aug[1] == 'h')
  415.     {
  416.       fs->eh_ptr = read_pointer (p);
  417.       p += sizeof (void *);
  418.       aug += 2;
  419.     }
  420.  
  421.   /* After the augmentation resp. pointer for "eh" augmentation
  422.      follows for CIE version >= 4 address size byte and
  423.      segment size byte.  */
  424.   if (__builtin_expect (cie->version >= 4, 0))
  425.     {
  426.       if (p[0] != sizeof (void *) || p[1] != 0)
  427.         return NULL;
  428.       p += 2;
  429.     }
  430.   /* Immediately following this are the code and
  431.      data alignment and return address column.  */
  432.   p = read_uleb128 (p, &utmp);
  433.   fs->code_align = (_Unwind_Word)utmp;
  434.   p = read_sleb128 (p, &stmp);
  435.   fs->data_align = (_Unwind_Sword)stmp;
  436.   if (cie->version == 1)
  437.     fs->retaddr_column = *p++;
  438.   else
  439.     {
  440.       p = read_uleb128 (p, &utmp);
  441.       fs->retaddr_column = (_Unwind_Word)utmp;
  442.     }
  443.   fs->lsda_encoding = DW_EH_PE_omit;
  444.  
  445.   /* If the augmentation starts with 'z', then a uleb128 immediately
  446.      follows containing the length of the augmentation field following
  447.      the size.  */
  448.   if (*aug == 'z')
  449.     {
  450.       p = read_uleb128 (p, &utmp);
  451.       ret = p + utmp;
  452.  
  453.       fs->saw_z = 1;
  454.       ++aug;
  455.     }
  456.  
  457.   /* Iterate over recognized augmentation subsequences.  */
  458.   while (*aug != '\0')
  459.     {
  460.       /* "L" indicates a byte showing how the LSDA pointer is encoded.  */
  461.       if (aug[0] == 'L')
  462.         {
  463.           fs->lsda_encoding = *p++;
  464.           aug += 1;
  465.         }
  466.  
  467.       /* "R" indicates a byte indicating how FDE addresses are encoded.  */
  468.       else if (aug[0] == 'R')
  469.         {
  470.           fs->fde_encoding = *p++;
  471.           aug += 1;
  472.         }
  473.  
  474.       /* "P" indicates a personality routine in the CIE augmentation.  */
  475.       else if (aug[0] == 'P')
  476.         {
  477.           _Unwind_Ptr personality;
  478.  
  479.           p = read_encoded_value (context, *p, p + 1, &personality);
  480.           fs->personality = (_Unwind_Personality_Fn) personality;
  481.           aug += 1;
  482.         }
  483.  
  484.       /* "S" indicates a signal frame.  */
  485.       else if (aug[0] == 'S')
  486.         {
  487.           fs->signal_frame = 1;
  488.           aug += 1;
  489.         }
  490.  
  491.       /* Otherwise we have an unknown augmentation string.
  492.          Bail unless we saw a 'z' prefix.  */
  493.       else
  494.         return ret;
  495.     }
  496.  
  497.   return ret ? ret : p;
  498. }
  499.  
  500.  
  501. /* Decode a DW_OP stack program.  Return the top of stack.  Push INITIAL
  502.    onto the stack to start.  */
  503.  
  504. static _Unwind_Word
  505. execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
  506.                   struct _Unwind_Context *context, _Unwind_Word initial)
  507. {
  508.   _Unwind_Word stack[64];       /* ??? Assume this is enough.  */
  509.   int stack_elt;
  510.  
  511.   stack[0] = initial;
  512.   stack_elt = 1;
  513.  
  514.   while (op_ptr < op_end)
  515.     {
  516.       enum dwarf_location_atom op = *op_ptr++;
  517.       _Unwind_Word result;
  518.       _uleb128_t reg, utmp;
  519.       _sleb128_t offset, stmp;
  520.  
  521.       switch (op)
  522.         {
  523.         case DW_OP_lit0:
  524.         case DW_OP_lit1:
  525.         case DW_OP_lit2:
  526.         case DW_OP_lit3:
  527.         case DW_OP_lit4:
  528.         case DW_OP_lit5:
  529.         case DW_OP_lit6:
  530.         case DW_OP_lit7:
  531.         case DW_OP_lit8:
  532.         case DW_OP_lit9:
  533.         case DW_OP_lit10:
  534.         case DW_OP_lit11:
  535.         case DW_OP_lit12:
  536.         case DW_OP_lit13:
  537.         case DW_OP_lit14:
  538.         case DW_OP_lit15:
  539.         case DW_OP_lit16:
  540.         case DW_OP_lit17:
  541.         case DW_OP_lit18:
  542.         case DW_OP_lit19:
  543.         case DW_OP_lit20:
  544.         case DW_OP_lit21:
  545.         case DW_OP_lit22:
  546.         case DW_OP_lit23:
  547.         case DW_OP_lit24:
  548.         case DW_OP_lit25:
  549.         case DW_OP_lit26:
  550.         case DW_OP_lit27:
  551.         case DW_OP_lit28:
  552.         case DW_OP_lit29:
  553.         case DW_OP_lit30:
  554.         case DW_OP_lit31:
  555.           result = op - DW_OP_lit0;
  556.           break;
  557.  
  558.         case DW_OP_addr:
  559.           result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
  560.           op_ptr += sizeof (void *);
  561.           break;
  562.  
  563.         case DW_OP_GNU_encoded_addr:
  564.           {
  565.             _Unwind_Ptr presult;
  566.             op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
  567.             result = presult;
  568.           }
  569.           break;
  570.  
  571.         case DW_OP_const1u:
  572.           result = read_1u (op_ptr);
  573.           op_ptr += 1;
  574.           break;
  575.         case DW_OP_const1s:
  576.           result = read_1s (op_ptr);
  577.           op_ptr += 1;
  578.           break;
  579.         case DW_OP_const2u:
  580.           result = read_2u (op_ptr);
  581.           op_ptr += 2;
  582.           break;
  583.         case DW_OP_const2s:
  584.           result = read_2s (op_ptr);
  585.           op_ptr += 2;
  586.           break;
  587.         case DW_OP_const4u:
  588.           result = read_4u (op_ptr);
  589.           op_ptr += 4;
  590.           break;
  591.         case DW_OP_const4s:
  592.           result = read_4s (op_ptr);
  593.           op_ptr += 4;
  594.           break;
  595.         case DW_OP_const8u:
  596.           result = read_8u (op_ptr);
  597.           op_ptr += 8;
  598.           break;
  599.         case DW_OP_const8s:
  600.           result = read_8s (op_ptr);
  601.           op_ptr += 8;
  602.           break;
  603.         case DW_OP_constu:
  604.           op_ptr = read_uleb128 (op_ptr, &utmp);
  605.           result = (_Unwind_Word)utmp;
  606.           break;
  607.         case DW_OP_consts:
  608.           op_ptr = read_sleb128 (op_ptr, &stmp);
  609.           result = (_Unwind_Sword)stmp;
  610.           break;
  611.  
  612.         case DW_OP_reg0:
  613.         case DW_OP_reg1:
  614.         case DW_OP_reg2:
  615.         case DW_OP_reg3:
  616.         case DW_OP_reg4:
  617.         case DW_OP_reg5:
  618.         case DW_OP_reg6:
  619.         case DW_OP_reg7:
  620.         case DW_OP_reg8:
  621.         case DW_OP_reg9:
  622.         case DW_OP_reg10:
  623.         case DW_OP_reg11:
  624.         case DW_OP_reg12:
  625.         case DW_OP_reg13:
  626.         case DW_OP_reg14:
  627.         case DW_OP_reg15:
  628.         case DW_OP_reg16:
  629.         case DW_OP_reg17:
  630.         case DW_OP_reg18:
  631.         case DW_OP_reg19:
  632.         case DW_OP_reg20:
  633.         case DW_OP_reg21:
  634.         case DW_OP_reg22:
  635.         case DW_OP_reg23:
  636.         case DW_OP_reg24:
  637.         case DW_OP_reg25:
  638.         case DW_OP_reg26:
  639.         case DW_OP_reg27:
  640.         case DW_OP_reg28:
  641.         case DW_OP_reg29:
  642.         case DW_OP_reg30:
  643.         case DW_OP_reg31:
  644.           result = _Unwind_GetGR (context, op - DW_OP_reg0);
  645.           break;
  646.         case DW_OP_regx:
  647.           op_ptr = read_uleb128 (op_ptr, &reg);
  648.           result = _Unwind_GetGR (context, reg);
  649.           break;
  650.  
  651.         case DW_OP_breg0:
  652.         case DW_OP_breg1:
  653.         case DW_OP_breg2:
  654.         case DW_OP_breg3:
  655.         case DW_OP_breg4:
  656.         case DW_OP_breg5:
  657.         case DW_OP_breg6:
  658.         case DW_OP_breg7:
  659.         case DW_OP_breg8:
  660.         case DW_OP_breg9:
  661.         case DW_OP_breg10:
  662.         case DW_OP_breg11:
  663.         case DW_OP_breg12:
  664.         case DW_OP_breg13:
  665.         case DW_OP_breg14:
  666.         case DW_OP_breg15:
  667.         case DW_OP_breg16:
  668.         case DW_OP_breg17:
  669.         case DW_OP_breg18:
  670.         case DW_OP_breg19:
  671.         case DW_OP_breg20:
  672.         case DW_OP_breg21:
  673.         case DW_OP_breg22:
  674.         case DW_OP_breg23:
  675.         case DW_OP_breg24:
  676.         case DW_OP_breg25:
  677.         case DW_OP_breg26:
  678.         case DW_OP_breg27:
  679.         case DW_OP_breg28:
  680.         case DW_OP_breg29:
  681.         case DW_OP_breg30:
  682.         case DW_OP_breg31:
  683.           op_ptr = read_sleb128 (op_ptr, &offset);
  684.           result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
  685.           break;
  686.         case DW_OP_bregx:
  687.           op_ptr = read_uleb128 (op_ptr, &reg);
  688.           op_ptr = read_sleb128 (op_ptr, &offset);
  689.           result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
  690.           break;
  691.  
  692.         case DW_OP_dup:
  693.           gcc_assert (stack_elt);
  694.           result = stack[stack_elt - 1];
  695.           break;
  696.  
  697.         case DW_OP_drop:
  698.           gcc_assert (stack_elt);
  699.           stack_elt -= 1;
  700.           goto no_push;
  701.  
  702.         case DW_OP_pick:
  703.           offset = *op_ptr++;
  704.           gcc_assert (offset < stack_elt - 1);
  705.           result = stack[stack_elt - 1 - offset];
  706.           break;
  707.  
  708.         case DW_OP_over:
  709.           gcc_assert (stack_elt >= 2);
  710.           result = stack[stack_elt - 2];
  711.           break;
  712.  
  713.         case DW_OP_swap:
  714.           {
  715.             _Unwind_Word t;
  716.             gcc_assert (stack_elt >= 2);
  717.             t = stack[stack_elt - 1];
  718.             stack[stack_elt - 1] = stack[stack_elt - 2];
  719.             stack[stack_elt - 2] = t;
  720.             goto no_push;
  721.           }
  722.  
  723.         case DW_OP_rot:
  724.           {
  725.             _Unwind_Word t1, t2, t3;
  726.  
  727.             gcc_assert (stack_elt >= 3);
  728.             t1 = stack[stack_elt - 1];
  729.             t2 = stack[stack_elt - 2];
  730.             t3 = stack[stack_elt - 3];
  731.             stack[stack_elt - 1] = t2;
  732.             stack[stack_elt - 2] = t3;
  733.             stack[stack_elt - 3] = t1;
  734.             goto no_push;
  735.           }
  736.  
  737.         case DW_OP_deref:
  738.         case DW_OP_deref_size:
  739.         case DW_OP_abs:
  740.         case DW_OP_neg:
  741.         case DW_OP_not:
  742.         case DW_OP_plus_uconst:
  743.           /* Unary operations.  */
  744.           gcc_assert (stack_elt);
  745.           stack_elt -= 1;
  746.  
  747.           result = stack[stack_elt];
  748.  
  749.           switch (op)
  750.             {
  751.             case DW_OP_deref:
  752.               {
  753.                 void *ptr = (void *) (_Unwind_Ptr) result;
  754.                 result = (_Unwind_Ptr) read_pointer (ptr);
  755.               }
  756.               break;
  757.  
  758.             case DW_OP_deref_size:
  759.               {
  760.                 void *ptr = (void *) (_Unwind_Ptr) result;
  761.                 switch (*op_ptr++)
  762.                   {
  763.                   case 1:
  764.                     result = read_1u (ptr);
  765.                     break;
  766.                   case 2:
  767.                     result = read_2u (ptr);
  768.                     break;
  769.                   case 4:
  770.                     result = read_4u (ptr);
  771.                     break;
  772.                   case 8:
  773.                     result = read_8u (ptr);
  774.                     break;
  775.                   default:
  776.                     gcc_unreachable ();
  777.                   }
  778.               }
  779.               break;
  780.  
  781.             case DW_OP_abs:
  782.               if ((_Unwind_Sword) result < 0)
  783.                 result = -result;
  784.               break;
  785.             case DW_OP_neg:
  786.               result = -result;
  787.               break;
  788.             case DW_OP_not:
  789.               result = ~result;
  790.               break;
  791.             case DW_OP_plus_uconst:
  792.               op_ptr = read_uleb128 (op_ptr, &utmp);
  793.               result += (_Unwind_Word)utmp;
  794.               break;
  795.  
  796.             default:
  797.               gcc_unreachable ();
  798.             }
  799.           break;
  800.  
  801.         case DW_OP_and:
  802.         case DW_OP_div:
  803.         case DW_OP_minus:
  804.         case DW_OP_mod:
  805.         case DW_OP_mul:
  806.         case DW_OP_or:
  807.         case DW_OP_plus:
  808.         case DW_OP_shl:
  809.         case DW_OP_shr:
  810.         case DW_OP_shra:
  811.         case DW_OP_xor:
  812.         case DW_OP_le:
  813.         case DW_OP_ge:
  814.         case DW_OP_eq:
  815.         case DW_OP_lt:
  816.         case DW_OP_gt:
  817.         case DW_OP_ne:
  818.           {
  819.             /* Binary operations.  */
  820.             _Unwind_Word first, second;
  821.             gcc_assert (stack_elt >= 2);
  822.             stack_elt -= 2;
  823.  
  824.             second = stack[stack_elt];
  825.             first = stack[stack_elt + 1];
  826.  
  827.             switch (op)
  828.               {
  829.               case DW_OP_and:
  830.                 result = second & first;
  831.                 break;
  832.               case DW_OP_div:
  833.                 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
  834.                 break;
  835.               case DW_OP_minus:
  836.                 result = second - first;
  837.                 break;
  838.               case DW_OP_mod:
  839.                 result = second % first;
  840.                 break;
  841.               case DW_OP_mul:
  842.                 result = second * first;
  843.                 break;
  844.               case DW_OP_or:
  845.                 result = second | first;
  846.                 break;
  847.               case DW_OP_plus:
  848.                 result = second + first;
  849.                 break;
  850.               case DW_OP_shl:
  851.                 result = second << first;
  852.                 break;
  853.               case DW_OP_shr:
  854.                 result = second >> first;
  855.                 break;
  856.               case DW_OP_shra:
  857.                 result = (_Unwind_Sword) second >> first;
  858.                 break;
  859.               case DW_OP_xor:
  860.                 result = second ^ first;
  861.                 break;
  862.               case DW_OP_le:
  863.                 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
  864.                 break;
  865.               case DW_OP_ge:
  866.                 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
  867.                 break;
  868.               case DW_OP_eq:
  869.                 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
  870.                 break;
  871.               case DW_OP_lt:
  872.                 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
  873.                 break;
  874.               case DW_OP_gt:
  875.                 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
  876.                 break;
  877.               case DW_OP_ne:
  878.                 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
  879.                 break;
  880.  
  881.               default:
  882.                 gcc_unreachable ();
  883.               }
  884.           }
  885.           break;
  886.  
  887.         case DW_OP_skip:
  888.           offset = read_2s (op_ptr);
  889.           op_ptr += 2;
  890.           op_ptr += offset;
  891.           goto no_push;
  892.  
  893.         case DW_OP_bra:
  894.           gcc_assert (stack_elt);
  895.           stack_elt -= 1;
  896.  
  897.           offset = read_2s (op_ptr);
  898.           op_ptr += 2;
  899.           if (stack[stack_elt] != 0)
  900.             op_ptr += offset;
  901.           goto no_push;
  902.  
  903.         case DW_OP_nop:
  904.           goto no_push;
  905.  
  906.         default:
  907.           gcc_unreachable ();
  908.         }
  909.  
  910.       /* Most things push a result value.  */
  911.       gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
  912.       stack[stack_elt++] = result;
  913.     no_push:;
  914.     }
  915.  
  916.   /* We were executing this program to get a value.  It should be
  917.      at top of stack.  */
  918.   gcc_assert (stack_elt);
  919.   stack_elt -= 1;
  920.   return stack[stack_elt];
  921. }
  922.  
  923.  
  924. /* Decode DWARF 2 call frame information. Takes pointers the
  925.    instruction sequence to decode, current register information and
  926.    CIE info, and the PC range to evaluate.  */
  927.  
  928. static void
  929. execute_cfa_program (const unsigned char *insn_ptr,
  930.                      const unsigned char *insn_end,
  931.                      struct _Unwind_Context *context,
  932.                      _Unwind_FrameState *fs)
  933. {
  934.   struct frame_state_reg_info *unused_rs = NULL;
  935.  
  936.   /* Don't allow remember/restore between CIE and FDE programs.  */
  937.   fs->regs.prev = NULL;
  938.  
  939.   /* The comparison with the return address uses < rather than <= because
  940.      we are only interested in the effects of code before the call; for a
  941.      noreturn function, the return address may point to unrelated code with
  942.      a different stack configuration that we are not interested in.  We
  943.      assume that the call itself is unwind info-neutral; if not, or if
  944.      there are delay instructions that adjust the stack, these must be
  945.      reflected at the point immediately before the call insn.
  946.      In signal frames, return address is after last completed instruction,
  947.      so we add 1 to return address to make the comparison <=.  */
  948.   while (insn_ptr < insn_end
  949.          && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
  950.     {
  951.       unsigned char insn = *insn_ptr++;
  952.       _uleb128_t reg, utmp;
  953.       _sleb128_t offset, stmp;
  954.  
  955.       if ((insn & 0xc0) == DW_CFA_advance_loc)
  956.         fs->pc += (insn & 0x3f) * fs->code_align;
  957.       else if ((insn & 0xc0) == DW_CFA_offset)
  958.         {
  959.           reg = insn & 0x3f;
  960.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  961.           offset = (_Unwind_Sword) utmp * fs->data_align;
  962.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  963.           if (UNWIND_COLUMN_IN_RANGE (reg))
  964.             {
  965.               fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  966.               fs->regs.reg[reg].loc.offset = offset;
  967.             }
  968.         }
  969.       else if ((insn & 0xc0) == DW_CFA_restore)
  970.         {
  971.           reg = insn & 0x3f;
  972.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  973.           if (UNWIND_COLUMN_IN_RANGE (reg))
  974.             fs->regs.reg[reg].how = REG_UNSAVED;
  975.         }
  976.       else switch (insn)
  977.         {
  978.         case DW_CFA_set_loc:
  979.           {
  980.             _Unwind_Ptr pc;
  981.  
  982.             insn_ptr = read_encoded_value (context, fs->fde_encoding,
  983.                                            insn_ptr, &pc);
  984.             fs->pc = (void *) pc;
  985.           }
  986.           break;
  987.  
  988.         case DW_CFA_advance_loc1:
  989.           fs->pc += read_1u (insn_ptr) * fs->code_align;
  990.           insn_ptr += 1;
  991.           break;
  992.         case DW_CFA_advance_loc2:
  993.           fs->pc += read_2u (insn_ptr) * fs->code_align;
  994.           insn_ptr += 2;
  995.           break;
  996.         case DW_CFA_advance_loc4:
  997.           fs->pc += read_4u (insn_ptr) * fs->code_align;
  998.           insn_ptr += 4;
  999.           break;
  1000.  
  1001.         case DW_CFA_offset_extended:
  1002.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1003.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1004.           offset = (_Unwind_Sword) utmp * fs->data_align;
  1005.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1006.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1007.             {
  1008.               fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  1009.               fs->regs.reg[reg].loc.offset = offset;
  1010.             }
  1011.           break;
  1012.  
  1013.         case DW_CFA_restore_extended:
  1014.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1015.           /* FIXME, this is wrong; the CIE might have said that the
  1016.              register was saved somewhere.  */
  1017.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1018.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1019.             fs->regs.reg[reg].how = REG_UNSAVED;
  1020.           break;
  1021.  
  1022.         case DW_CFA_same_value:
  1023.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1024.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1025.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1026.             fs->regs.reg[reg].how = REG_UNSAVED;
  1027.           break;
  1028.  
  1029.         case DW_CFA_undefined:
  1030.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1031.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1032.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1033.             fs->regs.reg[reg].how = REG_UNDEFINED;
  1034.           break;
  1035.  
  1036.         case DW_CFA_nop:
  1037.           break;
  1038.  
  1039.         case DW_CFA_register:
  1040.           {
  1041.             _uleb128_t reg2;
  1042.             insn_ptr = read_uleb128 (insn_ptr, &reg);
  1043.             insn_ptr = read_uleb128 (insn_ptr, &reg2);
  1044.             reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1045.             if (UNWIND_COLUMN_IN_RANGE (reg))
  1046.               {
  1047.                 fs->regs.reg[reg].how = REG_SAVED_REG;
  1048.                 fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2;
  1049.               }
  1050.           }
  1051.           break;
  1052.  
  1053.         case DW_CFA_remember_state:
  1054.           {
  1055.             struct frame_state_reg_info *new_rs;
  1056.             if (unused_rs)
  1057.               {
  1058.                 new_rs = unused_rs;
  1059.                 unused_rs = unused_rs->prev;
  1060.               }
  1061.             else
  1062.               new_rs = alloca (sizeof (struct frame_state_reg_info));
  1063.  
  1064.             *new_rs = fs->regs;
  1065.             fs->regs.prev = new_rs;
  1066.           }
  1067.           break;
  1068.  
  1069.         case DW_CFA_restore_state:
  1070.           {
  1071.             struct frame_state_reg_info *old_rs = fs->regs.prev;
  1072.             fs->regs = *old_rs;
  1073.             old_rs->prev = unused_rs;
  1074.             unused_rs = old_rs;
  1075.           }
  1076.           break;
  1077.  
  1078.         case DW_CFA_def_cfa:
  1079.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1080.           fs->regs.cfa_reg = (_Unwind_Word)utmp;
  1081.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1082.           fs->regs.cfa_offset = (_Unwind_Word)utmp;
  1083.           fs->regs.cfa_how = CFA_REG_OFFSET;
  1084.           break;
  1085.  
  1086.         case DW_CFA_def_cfa_register:
  1087.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1088.           fs->regs.cfa_reg = (_Unwind_Word)utmp;
  1089.           fs->regs.cfa_how = CFA_REG_OFFSET;
  1090.           break;
  1091.  
  1092.         case DW_CFA_def_cfa_offset:
  1093.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1094.           fs->regs.cfa_offset = utmp;
  1095.           /* cfa_how deliberately not set.  */
  1096.           break;
  1097.  
  1098.         case DW_CFA_def_cfa_expression:
  1099.           fs->regs.cfa_exp = insn_ptr;
  1100.           fs->regs.cfa_how = CFA_EXP;
  1101.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1102.           insn_ptr += utmp;
  1103.           break;
  1104.  
  1105.         case DW_CFA_expression:
  1106.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1107.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1108.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1109.             {
  1110.               fs->regs.reg[reg].how = REG_SAVED_EXP;
  1111.               fs->regs.reg[reg].loc.exp = insn_ptr;
  1112.             }
  1113.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1114.           insn_ptr += utmp;
  1115.           break;
  1116.  
  1117.           /* Dwarf3.  */
  1118.         case DW_CFA_offset_extended_sf:
  1119.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1120.           insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1121.           offset = stmp * fs->data_align;
  1122.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1123.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1124.             {
  1125.               fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  1126.               fs->regs.reg[reg].loc.offset = offset;
  1127.             }
  1128.           break;
  1129.  
  1130.         case DW_CFA_def_cfa_sf:
  1131.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1132.           fs->regs.cfa_reg = (_Unwind_Word)utmp;
  1133.           insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1134.           fs->regs.cfa_offset = (_Unwind_Sword)stmp;
  1135.           fs->regs.cfa_how = CFA_REG_OFFSET;
  1136.           fs->regs.cfa_offset *= fs->data_align;
  1137.           break;
  1138.  
  1139.         case DW_CFA_def_cfa_offset_sf:
  1140.           insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1141.           fs->regs.cfa_offset = (_Unwind_Sword)stmp;
  1142.           fs->regs.cfa_offset *= fs->data_align;
  1143.           /* cfa_how deliberately not set.  */
  1144.           break;
  1145.  
  1146.         case DW_CFA_val_offset:
  1147.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1148.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1149.           offset = (_Unwind_Sword) utmp * fs->data_align;
  1150.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1151.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1152.             {
  1153.               fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
  1154.               fs->regs.reg[reg].loc.offset = offset;
  1155.             }
  1156.           break;
  1157.  
  1158.         case DW_CFA_val_offset_sf:
  1159.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1160.           insn_ptr = read_sleb128 (insn_ptr, &stmp);
  1161.           offset = stmp * fs->data_align;
  1162.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1163.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1164.             {
  1165.               fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
  1166.               fs->regs.reg[reg].loc.offset = offset;
  1167.             }
  1168.           break;
  1169.  
  1170.         case DW_CFA_val_expression:
  1171.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1172.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1173.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1174.             {
  1175.               fs->regs.reg[reg].how = REG_SAVED_VAL_EXP;
  1176.               fs->regs.reg[reg].loc.exp = insn_ptr;
  1177.             }
  1178.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1179.           insn_ptr += utmp;
  1180.           break;
  1181.  
  1182.         case DW_CFA_GNU_window_save:
  1183.           /* ??? Hardcoded for SPARC register window configuration.  */
  1184.           if (__LIBGCC_DWARF_FRAME_REGISTERS__ >= 32)
  1185.             for (reg = 16; reg < 32; ++reg)
  1186.               {
  1187.                 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  1188.                 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
  1189.               }
  1190.           break;
  1191.  
  1192.         case DW_CFA_GNU_args_size:
  1193.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1194.           context->args_size = (_Unwind_Word)utmp;
  1195.           break;
  1196.  
  1197.         case DW_CFA_GNU_negative_offset_extended:
  1198.           /* Obsoleted by DW_CFA_offset_extended_sf, but used by
  1199.              older PowerPC code.  */
  1200.           insn_ptr = read_uleb128 (insn_ptr, &reg);
  1201.           insn_ptr = read_uleb128 (insn_ptr, &utmp);
  1202.           offset = (_Unwind_Word) utmp * fs->data_align;
  1203.           reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
  1204.           if (UNWIND_COLUMN_IN_RANGE (reg))
  1205.             {
  1206.               fs->regs.reg[reg].how = REG_SAVED_OFFSET;
  1207.               fs->regs.reg[reg].loc.offset = -offset;
  1208.             }
  1209.           break;
  1210.  
  1211.         default:
  1212.           gcc_unreachable ();
  1213.         }
  1214.     }
  1215. }
  1216. /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
  1217.    its caller and decode it into FS.  This function also sets the
  1218.    args_size and lsda members of CONTEXT, as they are really information
  1219.    about the caller's frame.  */
  1220.  
  1221. static _Unwind_Reason_Code
  1222. uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1223. {
  1224.   const struct dwarf_fde *fde;
  1225.   const struct dwarf_cie *cie;
  1226.   const unsigned char *aug, *insn, *end;
  1227.  
  1228.   memset (fs, 0, sizeof (*fs));
  1229.   context->args_size = 0;
  1230.   context->lsda = 0;
  1231.  
  1232.   if (context->ra == 0)
  1233.     return _URC_END_OF_STACK;
  1234.  
  1235.   fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
  1236.                           &context->bases);
  1237.   if (fde == NULL)
  1238.     {
  1239. #ifdef MD_FALLBACK_FRAME_STATE_FOR
  1240.       /* Couldn't find frame unwind info for this function.  Try a
  1241.          target-specific fallback mechanism.  This will necessarily
  1242.          not provide a personality routine or LSDA.  */
  1243.       return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
  1244. #else
  1245.       return _URC_END_OF_STACK;
  1246. #endif
  1247.     }
  1248.  
  1249.   fs->pc = context->bases.func;
  1250.  
  1251.   cie = get_cie (fde);
  1252.   insn = extract_cie_info (cie, context, fs);
  1253.   if (insn == NULL)
  1254.     /* CIE contained unknown augmentation.  */
  1255.     return _URC_FATAL_PHASE1_ERROR;
  1256.  
  1257.   /* First decode all the insns in the CIE.  */
  1258.   end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
  1259.   execute_cfa_program (insn, end, context, fs);
  1260.  
  1261.   /* Locate augmentation for the fde.  */
  1262.   aug = (const unsigned char *) fde + sizeof (*fde);
  1263.   aug += 2 * size_of_encoded_value (fs->fde_encoding);
  1264.   insn = NULL;
  1265.   if (fs->saw_z)
  1266.     {
  1267.       _uleb128_t i;
  1268.       aug = read_uleb128 (aug, &i);
  1269.       insn = aug + i;
  1270.     }
  1271.   if (fs->lsda_encoding != DW_EH_PE_omit)
  1272.     {
  1273.       _Unwind_Ptr lsda;
  1274.  
  1275.       aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
  1276.       context->lsda = (void *) lsda;
  1277.     }
  1278.  
  1279.   /* Then the insns in the FDE up to our target PC.  */
  1280.   if (insn == NULL)
  1281.     insn = aug;
  1282.   end = (const unsigned char *) next_fde (fde);
  1283.   execute_cfa_program (insn, end, context, fs);
  1284.  
  1285.   return _URC_NO_REASON;
  1286. }
  1287. typedef struct frame_state
  1288. {
  1289.   void *cfa;
  1290.   void *eh_ptr;
  1291.   long cfa_offset;
  1292.   long args_size;
  1293.   long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
  1294.   unsigned short cfa_reg;
  1295.   unsigned short retaddr_column;
  1296.   char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
  1297. } frame_state;
  1298.  
  1299. struct frame_state * __frame_state_for (void *, struct frame_state *);
  1300.  
  1301. /* Called from pre-G++ 3.0 __throw to find the registers to restore for
  1302.    a given PC_TARGET.  The caller should allocate a local variable of
  1303.    `struct frame_state' and pass its address to STATE_IN.  */
  1304.  
  1305. struct frame_state *
  1306. __frame_state_for (void *pc_target, struct frame_state *state_in)
  1307. {
  1308.   struct _Unwind_Context context;
  1309.   _Unwind_FrameState fs;
  1310.   int reg;
  1311.  
  1312.   memset (&context, 0, sizeof (struct _Unwind_Context));
  1313.   if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
  1314.     context.flags = EXTENDED_CONTEXT_BIT;
  1315.   context.ra = pc_target + 1;
  1316.  
  1317.   if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
  1318.     return 0;
  1319.  
  1320.   /* We have no way to pass a location expression for the CFA to our
  1321.      caller.  It wouldn't understand it anyway.  */
  1322.   if (fs.regs.cfa_how == CFA_EXP)
  1323.     return 0;
  1324.  
  1325.   for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
  1326.     {
  1327.       state_in->saved[reg] = fs.regs.reg[reg].how;
  1328.       switch (state_in->saved[reg])
  1329.         {
  1330.         case REG_SAVED_REG:
  1331.           state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
  1332.           break;
  1333.         case REG_SAVED_OFFSET:
  1334.           state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
  1335.           break;
  1336.         default:
  1337.           state_in->reg_or_offset[reg] = 0;
  1338.           break;
  1339.         }
  1340.     }
  1341.  
  1342.   state_in->cfa_offset = fs.regs.cfa_offset;
  1343.   state_in->cfa_reg = fs.regs.cfa_reg;
  1344.   state_in->retaddr_column = fs.retaddr_column;
  1345.   state_in->args_size = context.args_size;
  1346.   state_in->eh_ptr = fs.eh_ptr;
  1347.  
  1348.   return state_in;
  1349. }
  1350. typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
  1351.  
  1352. static inline void
  1353. _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
  1354.                      _Unwind_SpTmp *tmp_sp)
  1355. {
  1356.   int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
  1357.  
  1358.   if (size == sizeof(_Unwind_Ptr))
  1359.     tmp_sp->ptr = (_Unwind_Ptr) cfa;
  1360.   else
  1361.     {
  1362.       gcc_assert (size == sizeof(_Unwind_Word));
  1363.       tmp_sp->word = (_Unwind_Ptr) cfa;
  1364.     }
  1365.   _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
  1366. }
  1367.  
  1368. static void
  1369. uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1370. {
  1371.   struct _Unwind_Context orig_context = *context;
  1372.   void *cfa;
  1373.   long i;
  1374.  
  1375. #ifdef __LIBGCC_EH_RETURN_STACKADJ_RTX__
  1376.   /* Special handling here: Many machines do not use a frame pointer,
  1377.      and track the CFA only through offsets from the stack pointer from
  1378.      one frame to the next.  In this case, the stack pointer is never
  1379.      stored, so it has no saved address in the context.  What we do
  1380.      have is the CFA from the previous stack frame.
  1381.  
  1382.      In very special situations (such as unwind info for signal return),
  1383.      there may be location expressions that use the stack pointer as well.
  1384.  
  1385.      Do this conditionally for one frame.  This allows the unwind info
  1386.      for one frame to save a copy of the stack pointer from the previous
  1387.      frame, and be able to use much easier CFA mechanisms to do it.
  1388.      Always zap the saved stack pointer value for the next frame; carrying
  1389.      the value over from one frame to another doesn't make sense.  */
  1390.  
  1391.   _Unwind_SpTmp tmp_sp;
  1392.  
  1393.   if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
  1394.     _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
  1395.   _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
  1396. #endif
  1397.  
  1398.   /* Compute this frame's CFA.  */
  1399.   switch (fs->regs.cfa_how)
  1400.     {
  1401.     case CFA_REG_OFFSET:
  1402.       cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
  1403.       cfa += fs->regs.cfa_offset;
  1404.       break;
  1405.  
  1406.     case CFA_EXP:
  1407.       {
  1408.         const unsigned char *exp = fs->regs.cfa_exp;
  1409.         _uleb128_t len;
  1410.  
  1411.         exp = read_uleb128 (exp, &len);
  1412.         cfa = (void *) (_Unwind_Ptr)
  1413.           execute_stack_op (exp, exp + len, &orig_context, 0);
  1414.         break;
  1415.       }
  1416.  
  1417.     default:
  1418.       gcc_unreachable ();
  1419.     }
  1420.   context->cfa = cfa;
  1421.  
  1422.   /* Compute the addresses of all registers saved in this frame.  */
  1423.   for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__ + 1; ++i)
  1424.     switch (fs->regs.reg[i].how)
  1425.       {
  1426.       case REG_UNSAVED:
  1427.       case REG_UNDEFINED:
  1428.         break;
  1429.  
  1430.       case REG_SAVED_OFFSET:
  1431.         _Unwind_SetGRPtr (context, i,
  1432.                           (void *) (cfa + fs->regs.reg[i].loc.offset));
  1433.         break;
  1434.  
  1435.       case REG_SAVED_REG:
  1436.         if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
  1437.           _Unwind_SetGRValue (context, i,
  1438.                               _Unwind_GetGR (&orig_context,
  1439.                                              fs->regs.reg[i].loc.reg));
  1440.         else
  1441.           _Unwind_SetGRPtr (context, i,
  1442.                             _Unwind_GetGRPtr (&orig_context,
  1443.                                               fs->regs.reg[i].loc.reg));
  1444.         break;
  1445.  
  1446.       case REG_SAVED_EXP:
  1447.         {
  1448.           const unsigned char *exp = fs->regs.reg[i].loc.exp;
  1449.           _uleb128_t len;
  1450.           _Unwind_Ptr val;
  1451.  
  1452.           exp = read_uleb128 (exp, &len);
  1453.           val = execute_stack_op (exp, exp + len, &orig_context,
  1454.                                   (_Unwind_Ptr) cfa);
  1455.           _Unwind_SetGRPtr (context, i, (void *) val);
  1456.         }
  1457.         break;
  1458.  
  1459.       case REG_SAVED_VAL_OFFSET:
  1460.         _Unwind_SetGRValue (context, i,
  1461.                             (_Unwind_Internal_Ptr)
  1462.                             (cfa + fs->regs.reg[i].loc.offset));
  1463.         break;
  1464.  
  1465.       case REG_SAVED_VAL_EXP:
  1466.         {
  1467.           const unsigned char *exp = fs->regs.reg[i].loc.exp;
  1468.           _uleb128_t len;
  1469.           _Unwind_Ptr val;
  1470.  
  1471.           exp = read_uleb128 (exp, &len);
  1472.           val = execute_stack_op (exp, exp + len, &orig_context,
  1473.                                   (_Unwind_Ptr) cfa);
  1474.           _Unwind_SetGRValue (context, i, val);
  1475.         }
  1476.         break;
  1477.       }
  1478.  
  1479.   _Unwind_SetSignalFrame (context, fs->signal_frame);
  1480.  
  1481. #ifdef MD_FROB_UPDATE_CONTEXT
  1482.   MD_FROB_UPDATE_CONTEXT (context, fs);
  1483. #endif
  1484. }
  1485.  
  1486. /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
  1487.    of its caller.  Update CONTEXT to refer to the caller as well.  Note
  1488.    that the args_size and lsda members are not updated here, but later in
  1489.    uw_frame_state_for.  */
  1490.  
  1491. static void
  1492. uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1493. {
  1494.   uw_update_context_1 (context, fs);
  1495.  
  1496.   /* In general this unwinder doesn't make any distinction between
  1497.      undefined and same_value rule.  Call-saved registers are assumed
  1498.      to have same_value rule by default and explicit undefined
  1499.      rule is handled like same_value.  The only exception is
  1500.      DW_CFA_undefined on retaddr_column which is supposed to
  1501.      mark outermost frame in DWARF 3.  */
  1502.   if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
  1503.       == REG_UNDEFINED)
  1504.     /* uw_frame_state_for uses context->ra == 0 check to find outermost
  1505.        stack frame.  */
  1506.     context->ra = 0;
  1507.   else
  1508.     /* Compute the return address now, since the return address column
  1509.        can change from frame to frame.  */
  1510.     context->ra = __builtin_extract_return_addr
  1511.       (_Unwind_GetPtr (context, fs->retaddr_column));
  1512. }
  1513.  
  1514. static void
  1515. uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
  1516. {
  1517.   uw_update_context (context, fs);
  1518. }
  1519. /* Fill in CONTEXT for top-of-stack.  The only valid registers at this
  1520.    level will be the return address and the CFA.  */
  1521.  
  1522. #define uw_init_context(CONTEXT)                                           \
  1523.   do                                                                       \
  1524.     {                                                                      \
  1525.       /* Do any necessary initialization to access arbitrary stack frames. \
  1526.          On the SPARC, this means flushing the register windows.  */       \
  1527.       __builtin_unwind_init ();                                            \
  1528.       uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (),                  \
  1529.                          __builtin_return_address (0));                    \
  1530.     }                                                                      \
  1531.   while (0)
  1532.  
  1533. static inline void
  1534. init_dwarf_reg_size_table (void)
  1535. {
  1536.   __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
  1537. }
  1538.  
  1539. static void __attribute__((noinline))
  1540. uw_init_context_1 (struct _Unwind_Context *context,
  1541.                    void *outer_cfa, void *outer_ra)
  1542. {
  1543.   void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
  1544.   _Unwind_FrameState fs;
  1545.   _Unwind_SpTmp sp_slot;
  1546.   _Unwind_Reason_Code code;
  1547.  
  1548.   memset (context, 0, sizeof (struct _Unwind_Context));
  1549.   context->ra = ra;
  1550.   if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
  1551.     context->flags = EXTENDED_CONTEXT_BIT;
  1552.  
  1553.   code = uw_frame_state_for (context, &fs);
  1554.   gcc_assert (code == _URC_NO_REASON);
  1555.  
  1556. #if __GTHREADS
  1557.   {
  1558.     static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
  1559.     if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
  1560.         && dwarf_reg_size_table[0] == 0)
  1561.       init_dwarf_reg_size_table ();
  1562.   }
  1563. #else
  1564.   if (dwarf_reg_size_table[0] == 0)
  1565.     init_dwarf_reg_size_table ();
  1566. #endif
  1567.  
  1568.   /* Force the frame state to use the known cfa value.  */
  1569.   _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
  1570.   fs.regs.cfa_how = CFA_REG_OFFSET;
  1571.   fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
  1572.   fs.regs.cfa_offset = 0;
  1573.  
  1574.   uw_update_context_1 (context, &fs);
  1575.  
  1576.   /* If the return address column was saved in a register in the
  1577.      initialization context, then we can't see it in the given
  1578.      call frame data.  So have the initialization context tell us.  */
  1579.   context->ra = __builtin_extract_return_addr (outer_ra);
  1580. }
  1581.  
  1582. static void _Unwind_DebugHook (void *, void *)
  1583.   __attribute__ ((__noinline__, __used__, __noclone__));
  1584.  
  1585. /* This function is called during unwinding.  It is intended as a hook
  1586.    for a debugger to intercept exceptions.  CFA is the CFA of the
  1587.    target frame.  HANDLER is the PC to which control will be
  1588.    transferred.  */
  1589. static void
  1590. _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
  1591.                    void *handler __attribute__ ((__unused__)))
  1592. {
  1593.   /* We only want to use stap probes starting with v3.  Earlier
  1594.      versions added too much startup cost.  */
  1595. #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
  1596.   STAP_PROBE2 (libgcc, unwind, cfa, handler);
  1597. #else
  1598.   asm ("");
  1599. #endif
  1600. }
  1601.  
  1602. /* Install TARGET into CURRENT so that we can return to it.  This is a
  1603.    macro because __builtin_eh_return must be invoked in the context of
  1604.    our caller.  */
  1605.  
  1606. #define uw_install_context(CURRENT, TARGET)                             \
  1607.   do                                                                    \
  1608.     {                                                                   \
  1609.       long offset = uw_install_context_1 ((CURRENT), (TARGET));         \
  1610.       void *handler = __builtin_frob_return_addr ((TARGET)->ra);        \
  1611.       _Unwind_DebugHook ((TARGET)->cfa, handler);                       \
  1612.       __builtin_eh_return (offset, handler);                            \
  1613.     }                                                                   \
  1614.   while (0)
  1615.  
  1616. static long
  1617. uw_install_context_1 (struct _Unwind_Context *current,
  1618.                       struct _Unwind_Context *target)
  1619. {
  1620.   long i;
  1621.   _Unwind_SpTmp sp_slot;
  1622.  
  1623.   /* If the target frame does not have a saved stack pointer,
  1624.      then set up the target's CFA.  */
  1625.   if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
  1626.     _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
  1627.  
  1628.   for (i = 0; i < __LIBGCC_DWARF_FRAME_REGISTERS__; ++i)
  1629.     {
  1630.       void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
  1631.       void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
  1632.  
  1633.       gcc_assert (current->by_value[i] == 0);
  1634.       if (target->by_value[i] && c)
  1635.         {
  1636.           _Unwind_Word w;
  1637.           _Unwind_Ptr p;
  1638.           if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
  1639.             {
  1640.               w = (_Unwind_Internal_Ptr) t;
  1641.               memcpy (c, &w, sizeof (_Unwind_Word));
  1642.             }
  1643.           else
  1644.             {
  1645.               gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
  1646.               p = (_Unwind_Internal_Ptr) t;
  1647.               memcpy (c, &p, sizeof (_Unwind_Ptr));
  1648.             }
  1649.         }
  1650.       else if (t && c && t != c)
  1651.         memcpy (c, t, dwarf_reg_size_table[i]);
  1652.     }
  1653.  
  1654.   /* If the current frame doesn't have a saved stack pointer, then we
  1655.      need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
  1656.      pointer value reloaded.  */
  1657.   if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
  1658.     {
  1659.       void *target_cfa;
  1660.  
  1661.       target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
  1662.  
  1663.       /* We adjust SP by the difference between CURRENT and TARGET's CFA.  */
  1664.       if (__LIBGCC_STACK_GROWS_DOWNWARD__)
  1665.         return target_cfa - current->cfa + target->args_size;
  1666.       else
  1667.         return current->cfa - target_cfa - target->args_size;
  1668.     }
  1669.   return 0;
  1670. }
  1671.  
  1672. static inline _Unwind_Ptr
  1673. uw_identify_context (struct _Unwind_Context *context)
  1674. {
  1675.   /* The CFA is not sufficient to disambiguate the context of a function
  1676.      interrupted by a signal before establishing its frame and the context
  1677.      of the signal itself.  */
  1678.   if (__LIBGCC_STACK_GROWS_DOWNWARD__)
  1679.     return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
  1680.   else
  1681.     return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
  1682. }
  1683.  
  1684.  
  1685. #include "unwind.inc"
  1686.  
  1687. #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
  1688. alias (_Unwind_Backtrace);
  1689. alias (_Unwind_DeleteException);
  1690. alias (_Unwind_FindEnclosingFunction);
  1691. alias (_Unwind_ForcedUnwind);
  1692. alias (_Unwind_GetDataRelBase);
  1693. alias (_Unwind_GetTextRelBase);
  1694. alias (_Unwind_GetCFA);
  1695. alias (_Unwind_GetGR);
  1696. alias (_Unwind_GetIP);
  1697. alias (_Unwind_GetLanguageSpecificData);
  1698. alias (_Unwind_GetRegionStart);
  1699. alias (_Unwind_RaiseException);
  1700. alias (_Unwind_Resume);
  1701. alias (_Unwind_Resume_or_Rethrow);
  1702. alias (_Unwind_SetGR);
  1703. alias (_Unwind_SetIP);
  1704. #endif
  1705.  
  1706. #endif /* !USING_SJLJ_EXCEPTIONS */
  1707.