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