Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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