OSDN Git Service

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