OSDN Git Service

* gthr.h: Document __GTHREAD_MUTEX_INIT_FUNCTION.
[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 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
36 /* We disable this when inhibit_libc, so that gcc can still be built without
37    needing header files first.  */
38 /* ??? This is not a good solution, since prototypes may be required in
39    some cases for correct code.  See also libgcc2.c.  */
40 #ifndef inhibit_libc
41 /* fixproto guarantees these system headers exist. */
42 #include <stdlib.h>
43 #include <unistd.h>
44 #endif
45
46 #include "defaults.h"
47
48 #ifdef DWARF2_UNWIND_INFO
49 #include "gansidecl.h"
50 #include "dwarf2.h"
51 #include <stddef.h>
52 #include "frame.h"
53 #include "gthr.h"
54
55 #ifdef __GTHREAD_MUTEX_INIT
56 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
57 #else
58 static __gthread_mutex_t object_mutex;
59 #endif
60
61 /* Don't use `fancy_abort' here even if config.h says to use it.  */
62 #ifdef abort
63 #undef abort
64 #endif
65
66 /* Some types used by the DWARF 2 spec.  */
67
68 typedef          int  sword __attribute__ ((mode (SI)));
69 typedef unsigned int  uword __attribute__ ((mode (SI)));
70 typedef unsigned int  uaddr __attribute__ ((mode (pointer)));
71 typedef          int  saddr __attribute__ ((mode (pointer)));
72 typedef unsigned char ubyte;
73
74 /* The first few fields of a CIE.  The CIE_id field is 0 for a CIE,
75    to distinguish it from a valid FDE.  FDEs are aligned to an addressing
76    unit boundary, but the fields within are unaligned.  */
77
78 struct dwarf_cie {
79   uword length;
80   sword CIE_id;
81   ubyte version;
82   char augmentation[0];
83 } __attribute__ ((packed, aligned (__alignof__ (void *))));
84
85 /* The first few fields of an FDE.  */
86
87 struct dwarf_fde {
88   uword length;
89   sword CIE_delta;
90   void* pc_begin;
91   uaddr pc_range;
92 } __attribute__ ((packed, aligned (__alignof__ (void *))));
93
94 typedef struct dwarf_fde fde;
95
96 /* Objects to be searched for frame unwind info.  */
97
98 static struct object *objects;
99
100 /* The information we care about from a CIE.  */
101
102 struct cie_info {
103   char *augmentation;
104   void *eh_ptr;
105   int code_align;
106   int data_align;
107   unsigned ra_regno;
108 };
109
110 /* The current unwind state, plus a saved copy for DW_CFA_remember_state.  */
111
112 struct frame_state_internal
113 {
114   struct frame_state s;
115   struct frame_state_internal *saved_state;
116 };
117 \f
118 /* This is undefined below if we need it to be an actual function.  */
119 #define init_object_mutex_once()
120
121 #if __GTHREADS
122 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
123
124 /* Helper for init_object_mutex_once.  */
125
126 static void
127 init_object_mutex (void)
128 {
129   __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
130 }
131
132 /* Call this to arrange to initialize the object mutex.  */
133
134 #undef init_object_mutex_once
135 static void
136 init_object_mutex_once (void)
137 {
138   static __gthread_once_t once = __GTHREAD_ONCE_INIT;
139   __gthread_once (&once, init_object_mutex);
140 }
141
142 #endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
143 #endif /* __GTHREADS */
144 \f  
145 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
146    by R, and return the new value of BUF.  */
147
148 static void *
149 decode_uleb128 (unsigned char *buf, unsigned *r)
150 {
151   unsigned shift = 0;
152   unsigned result = 0;
153
154   while (1)
155     {
156       unsigned byte = *buf++;
157       result |= (byte & 0x7f) << shift;
158       if ((byte & 0x80) == 0)
159         break;
160       shift += 7;
161     }
162   *r = result;
163   return buf;
164 }
165
166 /* Decode the signed LEB128 constant at BUF into the variable pointed to
167    by R, and return the new value of BUF.  */
168
169 static void *
170 decode_sleb128 (unsigned char *buf, int *r)
171 {
172   unsigned shift = 0;
173   unsigned result = 0;
174   unsigned byte;
175
176   while (1)
177     {
178       byte = *buf++;
179       result |= (byte & 0x7f) << shift;
180       shift += 7;
181       if ((byte & 0x80) == 0)
182         break;
183     }
184   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
185     result |= - (1 << shift);
186
187   *r = result;
188   return buf;
189 }
190
191 /* Read unaligned data from the instruction buffer.  */
192
193 union unaligned {
194   void *p;
195   unsigned b2 __attribute__ ((mode (HI)));
196   unsigned b4 __attribute__ ((mode (SI)));
197   unsigned b8 __attribute__ ((mode (DI)));
198 } __attribute__ ((packed));
199 static inline void *
200 read_pointer (void *p)
201 { union unaligned *up = p; return up->p; }
202 static inline unsigned
203 read_1byte (void *p)
204 { return *(unsigned char *)p; }
205 static inline unsigned
206 read_2byte (void *p)
207 { union unaligned *up = p; return up->b2; }
208 static inline unsigned
209 read_4byte (void *p)
210 { union unaligned *up = p; return up->b4; }
211 static inline unsigned long
212 read_8byte (void *p)
213 { union unaligned *up = p; return up->b8; }
214 \f
215 /* Ordering function for FDEs.  Functions can't overlap, so we just compare
216    their starting addresses.  */
217
218 static inline saddr
219 fde_compare (fde *x, fde *y)
220 {
221   return (saddr)x->pc_begin - (saddr)y->pc_begin;
222 }
223
224 /* Return the address of the FDE after P.  */
225
226 static inline fde *
227 next_fde (fde *p)
228 {
229   return (fde *)(((char *)p) + p->length + sizeof (p->length));
230 }
231
232 /* Sorting an array of FDEs by address.
233    (Ideally we would have the linker sort the FDEs so we don't have to do
234    it at run time. But the linkers are not yet prepared for this.)  */
235
236 /* This is a special mix of insertion sort and heap sort, optimized for
237    the data sets that actually occur. They look like
238    101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
239    I.e. a linearly increasing sequence (coming from functions in the text
240    section), with additionally a few unordered elements (coming from functions
241    in gnu_linkonce sections) whose values are higher than the values in the
242    surrounding linear sequence (but not necessarily higher than the values
243    at the end of the linear sequence!).
244    The worst-case total run time is O(N) + O(n log (n)), where N is the
245    total number of FDEs and n is the number of erratic ones.  */
246
247 typedef struct fde_vector
248 {
249   fde **array;
250   size_t count;
251 } fde_vector;
252
253 typedef struct fde_accumulator
254 {
255   fde_vector linear;
256   fde_vector erratic;
257 } fde_accumulator;
258
259 static inline void
260 start_fde_sort (fde_accumulator *accu, size_t count)
261 {
262   accu->linear.array = (fde **) malloc (sizeof (fde *) * count);
263   accu->erratic.array = (fde **) malloc (sizeof (fde *) * count);
264   accu->linear.count = 0;
265   accu->erratic.count = 0;
266 }
267
268 static inline void
269 fde_insert (fde_accumulator *accu, fde *this_fde)
270 {
271   accu->linear.array[accu->linear.count++] = this_fde;
272 }
273
274 /* Split LINEAR into a linear sequence with low values and an erratic
275    sequence with high values, put the linear one (of longest possible
276    length) into LINEAR and the erratic one into ERRATIC. This is O(N).  */
277 static inline void
278 fde_split (fde_vector *linear, fde_vector *erratic)
279 {
280   size_t count = linear->count;
281   size_t linear_max = (size_t) -1;
282   size_t previous_max[count];
283   size_t i, j;
284
285   for (i = 0; i < count; i++)
286     {
287       for (j = linear_max;
288            j != (size_t) -1
289            && fde_compare (linear->array[i], linear->array[j]) < 0;
290            j = previous_max[j])
291         {
292           erratic->array[erratic->count++] = linear->array[j];
293           linear->array[j] = (fde *) NULL;
294         }
295       previous_max[i] = j;
296       linear_max = i;
297     }
298
299   for (i = 0, j = 0; i < count; i++)
300     if (linear->array[i] != (fde *) NULL)
301       linear->array[j++] = linear->array[i];
302   linear->count = j;
303 }
304
305 /* This is O(n log(n)).  BSD/OS defines heapsort in stdlib.h, so we must
306    use a name that does not conflict.  */
307 static inline void
308 frame_heapsort (fde_vector *erratic)
309 {
310   /* For a description of this algorithm, see:
311      Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
312      p. 60-61. */
313   fde ** a = erratic->array;
314   /* A portion of the array is called a "heap" if for all i>=0:
315      If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
316      If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
317 #define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0)
318   size_t n = erratic->count;
319   size_t m = n;
320   size_t i;
321
322   while (m > 0)
323     {
324       /* Invariant: a[m..n-1] is a heap. */
325       m--;
326       for (i = m; 2*i+1 < n; )
327         {
328           if (2*i+2 < n
329               && fde_compare (a[2*i+2], a[2*i+1]) > 0
330               && fde_compare (a[2*i+2], a[i]) > 0)
331             {
332               SWAP (a[i], a[2*i+2]);
333               i = 2*i+2;
334             }
335           else if (fde_compare (a[2*i+1], a[i]) > 0)
336             {
337               SWAP (a[i], a[2*i+1]);
338               i = 2*i+1;
339             }
340           else
341             break;
342         }
343     }
344   while (n > 1)
345     {
346       /* Invariant: a[0..n-1] is a heap. */
347       n--;
348       SWAP (a[0], a[n]);
349       for (i = 0; 2*i+1 < n; )
350         {
351           if (2*i+2 < n
352               && fde_compare (a[2*i+2], a[2*i+1]) > 0
353               && fde_compare (a[2*i+2], a[i]) > 0)
354             {
355               SWAP (a[i], a[2*i+2]);
356               i = 2*i+2;
357             }
358           else if (fde_compare (a[2*i+1], a[i]) > 0)
359             {
360               SWAP (a[i], a[2*i+1]);
361               i = 2*i+1;
362             }
363           else
364             break;
365         }
366     }
367 #undef SWAP
368 }
369
370 /* Merge V1 and V2, both sorted, and put the result into V1. */
371 static void
372 fde_merge (fde_vector *v1, const fde_vector *v2)
373 {
374   size_t i1, i2;
375   fde * fde2;
376
377   i2 = v2->count;
378   if (i2 > 0)
379     {
380       i1 = v1->count;
381       do {
382         i2--;
383         fde2 = v2->array[i2];
384         while (i1 > 0 && fde_compare (v1->array[i1-1], fde2) > 0)
385           {
386             v1->array[i1+i2] = v1->array[i1-1];
387             i1--;
388           }
389         v1->array[i1+i2] = fde2;
390       } while (i2 > 0);
391       v1->count += v2->count;
392     }
393 }
394
395 static fde **
396 end_fde_sort (fde_accumulator *accu, size_t count)
397 {
398   if (accu->linear.count != count)
399     abort ();
400   fde_split (&accu->linear, &accu->erratic);
401   if (accu->linear.count + accu->erratic.count != count)
402     abort ();
403   frame_heapsort (&accu->erratic);
404   fde_merge (&accu->linear, &accu->erratic);
405   free (accu->erratic.array);
406   return accu->linear.array;
407 }
408
409 static size_t
410 count_fdes (fde *this_fde)
411 {
412   size_t count;
413
414   for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
415     {
416       /* Skip CIEs and linked once FDE entries.  */
417       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
418         continue;
419
420       ++count;
421     }
422
423   return count;
424 }
425
426 static void
427 add_fdes (fde *this_fde, fde_accumulator *accu, void **beg_ptr, void **end_ptr)
428 {
429   void *pc_begin = *beg_ptr;
430   void *pc_end = *end_ptr;
431
432   for (; this_fde->length != 0; this_fde = next_fde (this_fde))
433     {
434       /* Skip CIEs and linked once FDE entries.  */
435       if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
436         continue;
437
438       fde_insert (accu, this_fde);
439
440       if (this_fde->pc_begin < pc_begin)
441         pc_begin = this_fde->pc_begin;
442       if (this_fde->pc_begin + this_fde->pc_range > pc_end)
443         pc_end = this_fde->pc_begin + this_fde->pc_range;
444     }
445
446   *beg_ptr = pc_begin;
447   *end_ptr = pc_end;
448 }
449
450 /* Set up a sorted array of pointers to FDEs for a loaded object.  We
451    count up the entries before allocating the array because it's likely to
452    be faster.  */
453
454 static void
455 frame_init (struct object* ob)
456 {
457   size_t count;
458   fde_accumulator accu;
459   void *pc_begin, *pc_end;
460
461   if (ob->fde_array)
462     {
463       fde **p = ob->fde_array;
464       for (count = 0; *p; ++p)
465         count += count_fdes (*p);
466     }
467   else
468     count = count_fdes (ob->fde_begin);
469
470   ob->count = count;
471
472   start_fde_sort (&accu, count);
473   pc_begin = (void*)(uaddr)-1;
474   pc_end = 0;
475
476   if (ob->fde_array)
477     {
478       fde **p = ob->fde_array;
479       for (; *p; ++p)
480         add_fdes (*p, &accu, &pc_begin, &pc_end);
481     }
482   else
483     add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
484
485   ob->fde_array = end_fde_sort (&accu, count);
486   ob->pc_begin = pc_begin;
487   ob->pc_end = pc_end;
488 }
489
490 /* Return a pointer to the FDE for the function containing PC.  */
491
492 static fde *
493 find_fde (void *pc)
494 {
495   struct object *ob;
496   size_t lo, hi;
497
498   init_object_mutex_once ();
499   __gthread_mutex_lock (&object_mutex);
500
501   for (ob = objects; ob; ob = ob->next)
502     {
503       if (ob->pc_begin == 0)
504         frame_init (ob);
505       if (pc >= ob->pc_begin && pc < ob->pc_end)
506         break;
507     }
508
509   __gthread_mutex_unlock (&object_mutex);
510
511   if (ob == 0)
512     return 0;
513
514   /* Standard binary search algorithm.  */
515   for (lo = 0, hi = ob->count; lo < hi; )
516     {
517       size_t i = (lo + hi) / 2;
518       fde *f = ob->fde_array[i];
519
520       if (pc < f->pc_begin)
521         hi = i;
522       else if (pc >= f->pc_begin + f->pc_range)
523         lo = i + 1;
524       else
525         return f;
526     }
527
528   return 0;
529 }
530 \f
531 static inline struct dwarf_cie *
532 get_cie (fde *f)
533 {
534   return ((void *)&f->CIE_delta) - f->CIE_delta;
535 }
536
537 /* Extract any interesting information from the CIE for the translation
538    unit F belongs to.  */
539
540 static void *
541 extract_cie_info (fde *f, struct cie_info *c)
542 {
543   void *p;
544   int i;
545
546   c->augmentation = get_cie (f)->augmentation;
547
548   if (strcmp (c->augmentation, "") != 0
549       && strcmp (c->augmentation, "eh") != 0
550       && c->augmentation[0] != 'z')
551     return 0;
552
553   p = c->augmentation + strlen (c->augmentation) + 1;
554
555   if (strcmp (c->augmentation, "eh") == 0)
556     {
557       c->eh_ptr = read_pointer (p);
558       p += sizeof (void *);
559     }
560   else
561     c->eh_ptr = 0;
562
563   p = decode_uleb128 (p, &c->code_align);
564   p = decode_sleb128 (p, &c->data_align);
565   c->ra_regno = *(unsigned char *)p++;
566
567   /* If the augmentation starts with 'z', we now see the length of the
568      augmentation fields.  */
569   if (c->augmentation[0] == 'z')
570     {
571       p = decode_uleb128 (p, &i);
572       p += i;
573     }
574
575   return p;
576 }
577
578 /* Decode one instruction's worth of DWARF 2 call frame information.
579    Used by __frame_state_for.  Takes pointers P to the instruction to
580    decode, STATE to the current register unwind information, INFO to the
581    current CIE information, and PC to the current PC value.  Returns a
582    pointer to the next instruction.  */
583
584 static void *
585 execute_cfa_insn (void *p, struct frame_state_internal *state,
586                   struct cie_info *info, void **pc)
587 {
588   unsigned insn = *(unsigned char *)p++;
589   unsigned reg;
590   int offset;
591
592   if (insn & DW_CFA_advance_loc)
593     *pc += ((insn & 0x3f) * info->code_align);
594   else if (insn & DW_CFA_offset)
595     {
596       reg = (insn & 0x3f);
597       p = decode_uleb128 (p, &offset);
598       offset *= info->data_align;
599       state->s.saved[reg] = REG_SAVED_OFFSET;
600       state->s.reg_or_offset[reg] = offset;
601     }
602   else if (insn & DW_CFA_restore)
603     {
604       reg = (insn & 0x3f);
605       state->s.saved[reg] = REG_UNSAVED;
606     }
607   else switch (insn)
608     {
609     case DW_CFA_set_loc:
610       *pc = read_pointer (p);
611       p += sizeof (void *);
612       break;
613     case DW_CFA_advance_loc1:
614       *pc += read_1byte (p);
615       p += 1;
616       break;
617     case DW_CFA_advance_loc2:
618       *pc += read_2byte (p);
619       p += 2;
620       break;
621     case DW_CFA_advance_loc4:
622       *pc += read_4byte (p);
623       p += 4;
624       break;
625
626     case DW_CFA_offset_extended:
627       p = decode_uleb128 (p, &reg);
628       p = decode_uleb128 (p, &offset);
629       offset *= info->data_align;
630       state->s.saved[reg] = REG_SAVED_OFFSET;
631       state->s.reg_or_offset[reg] = offset;
632       break;
633     case DW_CFA_restore_extended:
634       p = decode_uleb128 (p, &reg);
635       state->s.saved[reg] = REG_UNSAVED;
636       break;
637
638     case DW_CFA_undefined:
639     case DW_CFA_same_value:
640     case DW_CFA_nop:
641       break;
642
643     case DW_CFA_register:
644       {
645         unsigned reg2;
646         p = decode_uleb128 (p, &reg);
647         p = decode_uleb128 (p, &reg2);
648         state->s.saved[reg] = REG_SAVED_REG;
649         state->s.reg_or_offset[reg] = reg2;
650       }
651       break;
652
653     case DW_CFA_def_cfa:
654       p = decode_uleb128 (p, &reg);
655       p = decode_uleb128 (p, &offset);
656       state->s.cfa_reg = reg;
657       state->s.cfa_offset = offset;
658       break;
659     case DW_CFA_def_cfa_register:
660       p = decode_uleb128 (p, &reg);
661       state->s.cfa_reg = reg;
662       break;
663     case DW_CFA_def_cfa_offset:
664       p = decode_uleb128 (p, &offset);
665       state->s.cfa_offset = offset;
666       break;
667       
668     case DW_CFA_remember_state:
669       {
670         struct frame_state_internal *save =
671           (struct frame_state_internal *)
672           malloc (sizeof (struct frame_state_internal));
673         memcpy (save, state, sizeof (struct frame_state_internal));
674         state->saved_state = save;
675       }
676       break;
677     case DW_CFA_restore_state:
678       {
679         struct frame_state_internal *save = state->saved_state;
680         memcpy (state, save, sizeof (struct frame_state_internal));
681         free (save);
682       }
683       break;
684
685       /* FIXME: Hardcoded for SPARC register window configuration.  */
686     case DW_CFA_GNU_window_save:
687       for (reg = 16; reg < 32; ++reg)
688         {
689           state->s.saved[reg] = REG_SAVED_OFFSET;
690           state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
691         }
692       break;
693
694     case DW_CFA_GNU_args_size:
695       p = decode_uleb128 (p, &offset);
696       state->s.args_size = offset;
697       break;
698
699     default:
700       abort ();
701     }
702   return p;
703 }
704 \f
705 /* Called from crtbegin.o to register the unwind info for an object.  */
706
707 void
708 __register_frame_info (void *begin, struct object *ob)
709 {
710   ob->fde_begin = begin;
711
712   ob->pc_begin = ob->pc_end = 0;
713   ob->fde_array = 0;
714   ob->count = 0;
715
716   init_object_mutex_once ();
717   __gthread_mutex_lock (&object_mutex);
718
719   ob->next = objects;
720   objects = ob;
721
722   __gthread_mutex_unlock (&object_mutex);
723 }
724
725 void
726 __register_frame (void *begin)
727 {
728   struct object *ob = (struct object *) malloc (sizeof (struct object));
729   __register_frame_info (begin, ob);                       
730 }
731
732 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
733    for different translation units.  Called from the file generated by
734    collect2.  */
735
736 void
737 __register_frame_info_table (void *begin, struct object *ob)
738 {
739   ob->fde_begin = begin;
740   ob->fde_array = begin;
741
742   ob->pc_begin = ob->pc_end = 0;
743   ob->count = 0;
744
745   init_object_mutex_once ();
746   __gthread_mutex_lock (&object_mutex);
747
748   ob->next = objects;
749   objects = ob;
750
751   __gthread_mutex_unlock (&object_mutex);
752 }
753
754 void
755 __register_frame_table (void *begin)
756 {
757   struct object *ob = (struct object *) malloc (sizeof (struct object));
758   __register_frame_info_table (begin, ob);
759 }
760
761 /* Called from crtbegin.o to deregister the unwind info for an object.  */
762
763 void *
764 __deregister_frame_info (void *begin)
765 {
766   struct object **p;
767
768   init_object_mutex_once ();
769   __gthread_mutex_lock (&object_mutex);
770
771   p = &objects;
772   while (*p)
773     {
774       if ((*p)->fde_begin == begin)
775         {
776           struct object *ob = *p;
777           *p = (*p)->next;
778
779           /* If we've run init_frame for this object, free the FDE array.  */
780           if (ob->pc_begin)
781             free (ob->fde_array);
782
783           __gthread_mutex_unlock (&object_mutex);
784           return (void *) ob;
785         }
786       p = &((*p)->next);
787     }
788
789   __gthread_mutex_unlock (&object_mutex);
790   abort ();
791 }
792
793 void
794 __deregister_frame (void *begin)
795 {
796   free (__deregister_frame_info (begin));
797 }
798
799 /* Called from __throw to find the registers to restore for a given
800    PC_TARGET.  The caller should allocate a local variable of `struct
801    frame_state' (declared in frame.h) and pass its address to STATE_IN.  */
802
803 struct frame_state *
804 __frame_state_for (void *pc_target, struct frame_state *state_in)
805 {
806   fde *f;
807   void *insn, *end, *pc;
808   struct cie_info info;
809   struct frame_state_internal state;
810
811   f = find_fde (pc_target);
812   if (f == 0)
813     return 0;
814
815   insn = extract_cie_info (f, &info);
816   if (insn == 0)
817     return 0;
818
819   memset (&state, 0, sizeof (state));
820   state.s.retaddr_column = info.ra_regno;
821   state.s.eh_ptr = info.eh_ptr;
822
823   /* First decode all the insns in the CIE.  */
824   end = next_fde ((fde*) get_cie (f));
825   while (insn < end)
826     insn = execute_cfa_insn (insn, &state, &info, 0);
827
828   insn = ((fde *)f) + 1;
829
830   if (info.augmentation[0] == 'z')
831     {
832       int i;
833       insn = decode_uleb128 (insn, &i);
834       insn += i;
835     }
836
837   /* Then the insns in the FDE up to our target PC.  */
838   end = next_fde (f);
839   pc = f->pc_begin;
840   while (insn < end && pc <= pc_target)
841     insn = execute_cfa_insn (insn, &state, &info, &pc);
842
843   memcpy (state_in, &state.s, sizeof (state.s));
844   return state_in;
845 }
846 #endif /* DWARF2_UNWIND_INFO */