OSDN Git Service

* decl.c (init_decl_processing): Remove duplicate decl of
[pf3gnuchains/gcc-fork.git] / gcc / frame.c
1 /* Subroutines needed for unwinding stack frames for exception handling.  */
2 /* Compile this one with gcc.  */
3 /* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4    Contributed by Jason Merrill <jason@cygnus.com>.
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* As a special exception, if you link this library with other files,
24    some of which are compiled with GCC, to produce an executable,
25    this library does not by itself cause the resulting executable
26    to be covered by the GNU General Public License.
27    This exception does not however invalidate any other reasons why
28    the executable file might be covered by the GNU General Public License.  */
29
30 /* It is incorrect to include config.h here, because this file is being
31    compiled for the target, and hence definitions concerning only the host
32    do not apply.  */
33
34 #include "tconfig.h"
35 #include "tsystem.h"
36
37 #include "defaults.h"
38
39 #ifdef DWARF2_UNWIND_INFO
40 #include "dwarf2.h"
41 #include "frame.h"
42 #include "gthr.h"
43
44 #ifdef __GTHREAD_MUTEX_INIT
45 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
46 #else
47 static __gthread_mutex_t object_mutex;
48 #endif
49
50 /* Don't use `fancy_abort' here even if config.h says to use it.  */
51 #ifdef abort
52 #undef abort
53 #endif
54
55 /* Some types used by the DWARF 2 spec.  */
56
57 typedef          int  sword __attribute__ ((mode (SI)));
58 typedef unsigned int  uword __attribute__ ((mode (SI)));
59 typedef unsigned int  uaddr __attribute__ ((mode (pointer)));
60 typedef          int  saddr __attribute__ ((mode (pointer)));
61 typedef unsigned char ubyte;
62
63 /* Terminology:
64    CIE - Common Information Element
65    FDE - Frame Descriptor Element
66
67    There is one per function, and it describes where the function code
68    is located, and what the register lifetimes and stack layout are
69    within the function.
70
71    The data structures are defined in the DWARF specfication, although
72    not in a very readable way (see LITERATURE).
73
74    Every time an exception is thrown, the code needs to locate the FDE
75    for the current function, and starts to look for exception regions
76    from that FDE. This works in a two-level search:
77    a) in a linear search, find the shared image (i.e. DLL) containing
78       the PC
79    b) using the FDE table for that shared object, locate the FDE using
80       binary search (which requires the sorting).  */   
81
82 /* The first few fields of a CIE.  The CIE_id field is 0 for a CIE,
83    to distinguish it from a valid FDE.  FDEs are aligned to an addressing
84    unit boundary, but the fields within are unaligned.  */
85
86 struct dwarf_cie {
87   uword length;
88   sword CIE_id;
89   ubyte version;
90   char augmentation[0];
91 } __attribute__ ((packed, aligned (__alignof__ (void *))));
92
93 /* The first few fields of an FDE.  */
94
95 struct dwarf_fde {
96   uword length;
97   sword CIE_delta;
98   void* pc_begin;
99   uaddr pc_range;
100 } __attribute__ ((packed, aligned (__alignof__ (void *))));
101
102 typedef struct dwarf_fde fde;
103
104 /* Objects to be searched for frame unwind info.  */
105
106 static struct object *objects;
107
108 /* The information we care about from a CIE.  */
109
110 struct cie_info {
111   char *augmentation;
112   void *eh_ptr;
113   int code_align;
114   int data_align;
115   unsigned ra_regno;
116 };
117
118 /* The current unwind state, plus a saved copy for DW_CFA_remember_state.  */
119
120 struct frame_state_internal
121 {
122   struct frame_state s;
123   struct frame_state_internal *saved_state;
124 };
125 \f
126 /* This is undefined below if we need it to be an actual function.  */
127 #define init_object_mutex_once()
128
129 #if __GTHREADS
130 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
131
132 /* Helper for init_object_mutex_once.  */
133
134 static void
135 init_object_mutex (void)
136 {
137   __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
138 }
139
140 /* Call this to arrange to initialize the object mutex.  */
141
142 #undef init_object_mutex_once
143 static void
144 init_object_mutex_once (void)
145 {
146   static __gthread_once_t once = __GTHREAD_ONCE_INIT;
147   __gthread_once (&once, init_object_mutex);
148 }
149
150 #endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
151 #endif /* __GTHREADS */
152 \f  
153 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
154    by R, and return the new value of BUF.  */
155
156 static void *
157 decode_uleb128 (unsigned char *buf, unsigned *r)
158 {
159   unsigned shift = 0;
160   unsigned result = 0;
161
162   while (1)
163     {
164       unsigned byte = *buf++;
165       result |= (byte & 0x7f) << shift;
166       if ((byte & 0x80) == 0)
167         break;
168       shift += 7;
169     }
170   *r = result;
171   return buf;
172 }
173
174 /* Decode the signed LEB128 constant at BUF into the variable pointed to
175    by R, and return the new value of BUF.  */
176
177 static void *
178 decode_sleb128 (unsigned char *buf, int *r)
179 {
180   unsigned shift = 0;
181   unsigned result = 0;
182   unsigned byte;
183
184   while (1)
185     {
186       byte = *buf++;
187       result |= (byte & 0x7f) << shift;
188       shift += 7;
189       if ((byte & 0x80) == 0)
190         break;
191     }
192   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
193     result |= - (1 << shift);
194
195   *r = result;
196   return buf;
197 }
198
199 /* Read unaligned data from the instruction buffer.  */
200
201 union unaligned {
202   void *p;
203   unsigned b2 __attribute__ ((mode (HI)));
204   unsigned b4 __attribute__ ((mode (SI)));
205   unsigned b8 __attribute__ ((mode (DI)));
206 } __attribute__ ((packed));
207 static inline void *
208 read_pointer (void *p)
209 { union unaligned *up = p; return up->p; }
210 static inline unsigned
211 read_1byte (void *p)
212 { return *(unsigned char *)p; }
213 static inline unsigned
214 read_2byte (void *p)
215 { union unaligned *up = p; return up->b2; }
216 static inline unsigned
217 read_4byte (void *p)
218 { union unaligned *up = p; return up->b4; }
219 static inline unsigned long
220 read_8byte (void *p)
221 { union unaligned *up = p; return up->b8; }
222 \f
223 /* Ordering function for FDEs.  Functions can't overlap, so we just compare
224    their starting addresses.  */
225
226 static inline saddr
227 fde_compare (fde *x, fde *y)
228 {
229   return (saddr)x->pc_begin - (saddr)y->pc_begin;
230 }
231
232 /* Return the address of the FDE after P.  */
233
234 static inline fde *
235 next_fde (fde *p)
236 {
237   return (fde *)(((char *)p) + p->length + sizeof (p->length));
238 }
239
240 /* Sorting an array of FDEs by address.
241    (Ideally we would have the linker sort the FDEs so we don't have to do
242    it at run time. But the linkers are not yet prepared for this.)  */
243
244 /* This is a special mix of insertion sort and heap sort, optimized for
245    the data sets that actually occur. They look like
246    101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
247    I.e. a linearly increasing sequence (coming from functions in the text
248    section), with additionally a few unordered elements (coming from functions
249    in gnu_linkonce sections) whose values are higher than the values in the
250    surrounding linear sequence (but not necessarily higher than the values
251    at the end of the linear sequence!).
252    The worst-case total run time is O(N) + O(n log (n)), where N is the
253    total number of FDEs and n is the number of erratic ones.  */
254
255 typedef struct fde_vector
256 {
257   fde **array;
258   size_t count;
259 } fde_vector;
260
261 typedef struct fde_accumulator
262 {
263   fde_vector linear;
264   fde_vector erratic;
265 } fde_accumulator;
266
267 static inline int
268 start_fde_sort (fde_accumulator *accu, size_t count)
269 {
270   accu->linear.array = (fde **) malloc (sizeof (fde *) * count);
271   accu->erratic.array = accu->linear.array ?
272       (fde **) malloc (sizeof (fde *) * count) : NULL;
273   accu->linear.count = 0;
274   accu->erratic.count = 0;
275   
276   return accu->linear.array != NULL;
277 }
278
279 static inline void
280 fde_insert (fde_accumulator *accu, fde *this_fde)
281 {
282   if (accu->linear.array)
283     accu->linear.array[accu->linear.count++] = this_fde;
284 }
285
286 /* Split LINEAR into a linear sequence with low values and an erratic
287    sequence with high values, put the linear one (of longest possible
288    length) into LINEAR and the erratic one into ERRATIC. This is O(N).
289    
290    Because the longest linear sequence we are trying to locate within the
291    incoming LINEAR array can be interspersed with (high valued) erratic
292    entries.  We construct a chain indicating the sequenced entries.
293    To avoid having to allocate this chain, we overlay it onto the space of
294    the ERRATIC array during construction.  A final pass iterates over the
295    chain to determine what should be placed in the ERRATIC array, and
296    what is the linear sequence.  This overlay is safe from aliasing.  */
297 static inline void
298 fde_split (fde_vector *linear, fde_vector *erratic)
299 {
300   static fde *marker;
301   size_t count = linear->count;
302   fde **chain_end = &marker;
303   size_t i, j, k;
304
305   /* This should optimize out, but it is wise to make sure this assumption
306      is correct. Should these have different sizes, we cannot cast between
307      them and the overlaying onto ERRATIC will not work.  */
308   if (sizeof (fde *) != sizeof (fde **))
309     abort ();
310   
311   for (i = 0; i < count; i++)
312     {
313       fde **probe;
314       
315       for (probe = chain_end;
316            probe != &marker && fde_compare (linear->array[i], *probe) < 0;
317            probe = chain_end)
318         {
319           chain_end = (fde **)erratic->array[probe - linear->array];
320           erratic->array[probe - linear->array] = NULL;
321         }
322       erratic->array[i] = (fde *)chain_end;
323       chain_end = &linear->array[i];
324     }
325
326   /* Each entry in LINEAR which is part of the linear sequence we have
327      discovered will correspond to a non-NULL entry in the chain we built in
328      the ERRATIC array.  */
329   for (i = j = k = 0; i < count; i++)
330     if (erratic->array[i])
331       linear->array[j++] = linear->array[i];
332     else
333       erratic->array[k++] = linear->array[i];
334   linear->count = j;
335   erratic->count = k;
336 }
337
338 /* This is O(n log(n)).  BSD/OS defines heapsort in stdlib.h, so we must
339    use a name that does not conflict.  */
340 static inline void
341 frame_heapsort (fde_vector *erratic)
342 {
343   /* For a description of this algorithm, see:
344      Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
345      p. 60-61. */
346   fde ** a = erratic->array;
347   /* A portion of the array is called a "heap" if for all i>=0:
348      If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
349      If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
350 #define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0)
351   size_t n = erratic->count;
352   size_t m = n;
353   size_t i;
354
355   while (m > 0)
356     {
357       /* Invariant: a[m..n-1] is a heap. */
358       m--;
359       for (i = m; 2*i+1 < n; )
360         {
361           if (2*i+2 < n
362               && fde_compare (a[2*i+2], a[2*i+1]) > 0
363               && fde_compare (a[2*i+2], a[i]) > 0)
364             {
365               SWAP (a[i], a[2*i+2]);
366               i = 2*i+2;
367             }
368           else if (fde_compare (a[2*i+1], a[i]) > 0)
369             {
370               SWAP (a[i], a[2*i+1]);
371               i = 2*i+1;
372             }
373           else
374             break;
375         }
376     }
377   while (n > 1)
378     {
379       /* Invariant: a[0..n-1] is a heap. */
380       n--;
381       SWAP (a[0], a[n]);
382       for (i = 0; 2*i+1 < n; )
383         {
384           if (2*i+2 < n
385               && fde_compare (a[2*i+2], a[2*i+1]) > 0
386               && fde_compare (a[2*i+2], a[i]) > 0)
387             {
388               SWAP (a[i], a[2*i+2]);
389               i = 2*i+2;
390             }
391           else if (fde_compare (a[2*i+1], a[i]) > 0)
392             {
393               SWAP (a[i], a[2*i+1]);
394               i = 2*i+1;
395             }
396           else
397             break;
398         }
399     }
400 #undef SWAP
401 }
402
403 /* Merge V1 and V2, both sorted, and put the result into V1. */
404 static void
405 fde_merge (fde_vector *v1, const fde_vector *v2)
406 {
407   size_t i1, i2;
408   fde * fde2;
409
410   i2 = v2->count;
411   if (i2 > 0)
412     {
413       i1 = v1->count;
414       do {
415         i2--;
416         fde2 = v2->array[i2];
417         while (i1 > 0 && fde_compare (v1->array[i1-1], fde2) > 0)
418           {
419             v1->array[i1+i2] = v1->array[i1-1];
420             i1--;
421           }
422         v1->array[i1+i2] = fde2;
423       } while (i2 > 0);
424       v1->count += v2->count;
425     }
426 }
427
428 static fde **
429 end_fde_sort (fde_accumulator *accu, size_t count)
430 {
431   if (accu->linear.array && accu->linear.count != count)
432     abort ();
433   
434   if (accu->erratic.array)
435     {
436       fde_split (&accu->linear, &accu->erratic);
437       if (accu->linear.count + accu->erratic.count != count)
438         abort ();
439       frame_heapsort (&accu->erratic);
440       fde_merge (&accu->linear, &accu->erratic);
441       if (accu->erratic.array)
442         free (accu->erratic.array);
443     }
444   else
445     {
446       /* We've not managed to malloc an erratic array, so heap sort in the
447          linear one.  */
448       frame_heapsort (&accu->linear);
449     }
450   return accu->linear.array;
451 }
452
453 static size_t
454 count_fdes (fde *this_fde)
455 {
456   size_t count;
457
458   for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
459     {
460       /* Skip CIEs and linked once FDE entries.  */
461       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
462         continue;
463
464       ++count;
465     }
466
467   return count;
468 }
469
470 static void
471 add_fdes (fde *this_fde, fde_accumulator *accu, void **beg_ptr, void **end_ptr)
472 {
473   void *pc_begin = *beg_ptr;
474   void *pc_end = *end_ptr;
475
476   for (; this_fde->length != 0; this_fde = next_fde (this_fde))
477     {
478       /* Skip CIEs and linked once FDE entries.  */
479       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
480         continue;
481
482       fde_insert (accu, this_fde);
483
484       if (this_fde->pc_begin < pc_begin)
485         pc_begin = this_fde->pc_begin;
486       if (this_fde->pc_begin + this_fde->pc_range > pc_end)
487         pc_end = this_fde->pc_begin + this_fde->pc_range;
488     }
489
490   *beg_ptr = pc_begin;
491   *end_ptr = pc_end;
492 }
493
494 /* search this fde table for the one containing the pc */
495 static fde *
496 search_fdes (fde *this_fde, void *pc)
497 {
498   for (; this_fde->length != 0; this_fde = next_fde (this_fde))
499     {
500       /* Skip CIEs and linked once FDE entries.  */
501       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
502         continue;
503
504       if ((uaddr)((char *)pc - (char *)this_fde->pc_begin) < this_fde->pc_range)
505         return this_fde;
506     }
507   return NULL;
508 }
509
510 /* Set up a sorted array of pointers to FDEs for a loaded object.  We
511    count up the entries before allocating the array because it's likely to
512    be faster.  We can be called multiple times, should we have failed to
513    allocate a sorted fde array on a previous occasion.  */
514
515 static void
516 frame_init (struct object* ob)
517 {
518   size_t count;
519   fde_accumulator accu;
520   void *pc_begin, *pc_end;
521   fde **array;
522
523   if (ob->pc_begin)
524     count = ob->count;
525   else if (ob->fde_array)
526     {
527       fde **p = ob->fde_array;
528       for (count = 0; *p; ++p)
529         count += count_fdes (*p);
530     }
531   else
532     count = count_fdes (ob->fde_begin);
533   ob->count = count;
534
535   if (!start_fde_sort (&accu, count) && ob->pc_begin)
536     return;
537
538   pc_begin = (void*)(uaddr)-1;
539   pc_end = 0;
540
541   if (ob->fde_array)
542     {
543       fde **p = ob->fde_array;
544       for (; *p; ++p)
545         add_fdes (*p, &accu, &pc_begin, &pc_end);
546     }
547   else
548     add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
549
550   array = end_fde_sort (&accu, count);
551   if (array)
552     ob->fde_array = array;
553   ob->pc_begin = pc_begin;
554   ob->pc_end = pc_end;
555 }
556
557 /* Return a pointer to the FDE for the function containing PC.  */
558
559 static fde *
560 find_fde (void *pc)
561 {
562   struct object *ob;
563   size_t lo, hi;
564
565   init_object_mutex_once ();
566   __gthread_mutex_lock (&object_mutex);
567
568   /* Linear search through the objects, to find the one containing the pc. */
569   for (ob = objects; ob; ob = ob->next)
570     {
571       if (ob->pc_begin == 0)
572         frame_init (ob);
573       if (pc >= ob->pc_begin && pc < ob->pc_end)
574         break;
575     }
576
577   if (ob == 0)
578     {
579       __gthread_mutex_unlock (&object_mutex);
580       return 0;
581     }
582
583   if (!ob->fde_array || (void *)ob->fde_array == (void *)ob->fde_begin)
584     frame_init (ob);
585
586   if (ob->fde_array && (void *)ob->fde_array != (void *)ob->fde_begin)
587     {
588       __gthread_mutex_unlock (&object_mutex);
589       
590       /* Standard binary search algorithm.  */
591       for (lo = 0, hi = ob->count; lo < hi; )
592         {
593           size_t i = (lo + hi) / 2;
594           fde *f = ob->fde_array[i];
595
596           if (pc < f->pc_begin)
597             hi = i;
598           else if (pc >= f->pc_begin + f->pc_range)
599             lo = i + 1;
600           else
601             return f;
602         }
603     }
604   else
605     {
606       /* Long slow labourious linear search, cos we've no memory. */
607       fde *f;
608       
609       if (ob->fde_array)
610         {
611           fde **p = ob->fde_array;
612           
613           do
614             {
615               f = search_fdes (*p, pc);
616               if (f)
617                 break;
618               p++;
619             }
620           while (*p);
621         }
622       else
623         f = search_fdes (ob->fde_begin, pc);
624       __gthread_mutex_unlock (&object_mutex);
625       return f;
626     }
627   return 0;
628 }
629 \f
630 static inline struct dwarf_cie *
631 get_cie (fde *f)
632 {
633   return ((void *)&f->CIE_delta) - f->CIE_delta;
634 }
635
636 /* Extract any interesting information from the CIE for the translation
637    unit F belongs to.  */
638
639 static void *
640 extract_cie_info (fde *f, struct cie_info *c)
641 {
642   void *p;
643   int i;
644
645   c->augmentation = get_cie (f)->augmentation;
646
647   if (strcmp (c->augmentation, "") != 0
648       && strcmp (c->augmentation, "eh") != 0
649       && c->augmentation[0] != 'z')
650     return 0;
651
652   p = c->augmentation + strlen (c->augmentation) + 1;
653
654   if (strcmp (c->augmentation, "eh") == 0)
655     {
656       c->eh_ptr = read_pointer (p);
657       p += sizeof (void *);
658     }
659   else
660     c->eh_ptr = 0;
661
662   p = decode_uleb128 (p, &c->code_align);
663   p = decode_sleb128 (p, &c->data_align);
664   c->ra_regno = *(unsigned char *)p++;
665
666   /* If the augmentation starts with 'z', we now see the length of the
667      augmentation fields.  */
668   if (c->augmentation[0] == 'z')
669     {
670       p = decode_uleb128 (p, &i);
671       p += i;
672     }
673
674   return p;
675 }
676
677 /* Decode one instruction's worth of DWARF 2 call frame information.
678    Used by __frame_state_for.  Takes pointers P to the instruction to
679    decode, STATE to the current register unwind information, INFO to the
680    current CIE information, and PC to the current PC value.  Returns a
681    pointer to the next instruction.  */
682
683 static void *
684 execute_cfa_insn (void *p, struct frame_state_internal *state,
685                   struct cie_info *info, void **pc)
686 {
687   unsigned insn = *(unsigned char *)p++;
688   unsigned reg;
689   int offset;
690
691   if (insn & DW_CFA_advance_loc)
692     *pc += ((insn & 0x3f) * info->code_align);
693   else if (insn & DW_CFA_offset)
694     {
695       reg = (insn & 0x3f);
696       p = decode_uleb128 (p, &offset);
697       offset *= info->data_align;
698       state->s.saved[reg] = REG_SAVED_OFFSET;
699       state->s.reg_or_offset[reg] = offset;
700     }
701   else if (insn & DW_CFA_restore)
702     {
703       reg = (insn & 0x3f);
704       state->s.saved[reg] = REG_UNSAVED;
705     }
706   else switch (insn)
707     {
708     case DW_CFA_set_loc:
709       *pc = read_pointer (p);
710       p += sizeof (void *);
711       break;
712     case DW_CFA_advance_loc1:
713       *pc += read_1byte (p);
714       p += 1;
715       break;
716     case DW_CFA_advance_loc2:
717       *pc += read_2byte (p);
718       p += 2;
719       break;
720     case DW_CFA_advance_loc4:
721       *pc += read_4byte (p);
722       p += 4;
723       break;
724
725     case DW_CFA_offset_extended:
726       p = decode_uleb128 (p, &reg);
727       p = decode_uleb128 (p, &offset);
728       offset *= info->data_align;
729       state->s.saved[reg] = REG_SAVED_OFFSET;
730       state->s.reg_or_offset[reg] = offset;
731       break;
732     case DW_CFA_restore_extended:
733       p = decode_uleb128 (p, &reg);
734       state->s.saved[reg] = REG_UNSAVED;
735       break;
736
737     case DW_CFA_undefined:
738     case DW_CFA_same_value:
739     case DW_CFA_nop:
740       break;
741
742     case DW_CFA_register:
743       {
744         unsigned reg2;
745         p = decode_uleb128 (p, &reg);
746         p = decode_uleb128 (p, &reg2);
747         state->s.saved[reg] = REG_SAVED_REG;
748         state->s.reg_or_offset[reg] = reg2;
749       }
750       break;
751
752     case DW_CFA_def_cfa:
753       p = decode_uleb128 (p, &reg);
754       p = decode_uleb128 (p, &offset);
755       state->s.cfa_reg = reg;
756       state->s.cfa_offset = offset;
757       break;
758     case DW_CFA_def_cfa_register:
759       p = decode_uleb128 (p, &reg);
760       state->s.cfa_reg = reg;
761       break;
762     case DW_CFA_def_cfa_offset:
763       p = decode_uleb128 (p, &offset);
764       state->s.cfa_offset = offset;
765       break;
766       
767     case DW_CFA_remember_state:
768       {
769         struct frame_state_internal *save =
770           (struct frame_state_internal *)
771           malloc (sizeof (struct frame_state_internal));
772         memcpy (save, state, sizeof (struct frame_state_internal));
773         state->saved_state = save;
774       }
775       break;
776     case DW_CFA_restore_state:
777       {
778         struct frame_state_internal *save = state->saved_state;
779         memcpy (state, save, sizeof (struct frame_state_internal));
780         free (save);
781       }
782       break;
783
784       /* FIXME: Hardcoded for SPARC register window configuration.  */
785     case DW_CFA_GNU_window_save:
786       for (reg = 16; reg < 32; ++reg)
787         {
788           state->s.saved[reg] = REG_SAVED_OFFSET;
789           state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
790         }
791       break;
792
793     case DW_CFA_GNU_args_size:
794       p = decode_uleb128 (p, &offset);
795       state->s.args_size = offset;
796       break;
797
798     case DW_CFA_GNU_negative_offset_extended:
799       p = decode_uleb128 (p, &reg);
800       p = decode_uleb128 (p, &offset);
801       offset *= info->data_align;
802       state->s.saved[reg] = REG_SAVED_OFFSET;
803       state->s.reg_or_offset[reg] = -offset;
804       break;
805
806     default:
807       abort ();
808     }
809   return p;
810 }
811 \f
812 /* Called from crtbegin.o to register the unwind info for an object.  */
813
814 void
815 __register_frame_info (void *begin, struct object *ob)
816 {
817   ob->fde_begin = begin;
818
819   ob->pc_begin = ob->pc_end = 0;
820   ob->fde_array = 0;
821   ob->count = 0;
822
823   init_object_mutex_once ();
824   __gthread_mutex_lock (&object_mutex);
825
826   ob->next = objects;
827   objects = ob;
828
829   __gthread_mutex_unlock (&object_mutex);
830 }
831
832 void
833 __register_frame (void *begin)
834 {
835   struct object *ob = (struct object *) malloc (sizeof (struct object));
836   __register_frame_info (begin, ob);                       
837 }
838
839 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
840    for different translation units.  Called from the file generated by
841    collect2.  */
842
843 void
844 __register_frame_info_table (void *begin, struct object *ob)
845 {
846   ob->fde_begin = begin;
847   ob->fde_array = begin;
848
849   ob->pc_begin = ob->pc_end = 0;
850   ob->count = 0;
851
852   init_object_mutex_once ();
853   __gthread_mutex_lock (&object_mutex);
854
855   ob->next = objects;
856   objects = ob;
857
858   __gthread_mutex_unlock (&object_mutex);
859 }
860
861 void
862 __register_frame_table (void *begin)
863 {
864   struct object *ob = (struct object *) malloc (sizeof (struct object));
865   __register_frame_info_table (begin, ob);
866 }
867
868 /* Called from crtbegin.o to deregister the unwind info for an object.  */
869
870 void *
871 __deregister_frame_info (void *begin)
872 {
873   struct object **p;
874
875   init_object_mutex_once ();
876   __gthread_mutex_lock (&object_mutex);
877
878   p = &objects;
879   while (*p)
880     {
881       if ((*p)->fde_begin == begin)
882         {
883           struct object *ob = *p;
884           *p = (*p)->next;
885
886           /* If we've run init_frame for this object, free the FDE array.  */
887           if (ob->fde_array && ob->fde_array != begin)
888             free (ob->fde_array);
889
890           __gthread_mutex_unlock (&object_mutex);
891           return (void *) ob;
892         }
893       p = &((*p)->next);
894     }
895
896   __gthread_mutex_unlock (&object_mutex);
897   abort ();
898 }
899
900 void
901 __deregister_frame (void *begin)
902 {
903   free (__deregister_frame_info (begin));
904 }
905
906 /* Called from __throw to find the registers to restore for a given
907    PC_TARGET.  The caller should allocate a local variable of `struct
908    frame_state' (declared in frame.h) and pass its address to STATE_IN.  */
909
910 struct frame_state *
911 __frame_state_for (void *pc_target, struct frame_state *state_in)
912 {
913   fde *f;
914   void *insn, *end, *pc;
915   struct cie_info info;
916   struct frame_state_internal state;
917
918   f = find_fde (pc_target);
919   if (f == 0)
920     return 0;
921
922   insn = extract_cie_info (f, &info);
923   if (insn == 0)
924     return 0;
925
926   memset (&state, 0, sizeof (state));
927   state.s.retaddr_column = info.ra_regno;
928   state.s.eh_ptr = info.eh_ptr;
929
930   /* First decode all the insns in the CIE.  */
931   end = next_fde ((fde*) get_cie (f));
932   while (insn < end)
933     insn = execute_cfa_insn (insn, &state, &info, 0);
934
935   insn = ((fde *)f) + 1;
936
937   if (info.augmentation[0] == 'z')
938     {
939       int i;
940       insn = decode_uleb128 (insn, &i);
941       insn += i;
942     }
943
944   /* Then the insns in the FDE up to our target PC.  */
945   end = next_fde (f);
946   pc = f->pc_begin;
947   while (insn < end && pc <= pc_target)
948     insn = execute_cfa_insn (insn, &state, &info, &pc);
949
950   memcpy (state_in, &state.s, sizeof (state.s));
951   return state_in;
952 }
953 #endif /* DWARF2_UNWIND_INFO */