OSDN Git Service

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