OSDN Git Service

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