OSDN Git Service

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