* cp-tree.def (SCOPE_STMT): Take one operand.
[official-gcc.git] / gcc / frame.c
blobfa011d342b63657c9702616b00ddf1a68112dd60
1 /* Subroutines needed for unwinding stack frames for exception handling. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
4 Contributed by Jason Merrill <jason@cygnus.com>.
6 This file is part of GNU CC.
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.
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.
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. */
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. */
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. */
34 #include "tconfig.h"
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>
45 #else
46 #include <stddef.h>
47 #ifndef malloc
48 extern void *malloc (size_t);
49 #endif
50 #ifndef free
51 extern void free (void *);
52 #endif
53 #endif
55 #include "defaults.h"
57 #ifdef DWARF2_UNWIND_INFO
58 #include "dwarf2.h"
59 #include <stddef.h>
60 #include "frame.h"
61 #include "gthr.h"
63 #ifdef __GTHREAD_MUTEX_INIT
64 static __gthread_mutex_t object_mutex = __GTHREAD_MUTEX_INIT;
65 #else
66 static __gthread_mutex_t object_mutex;
67 #endif
69 /* Don't use `fancy_abort' here even if config.h says to use it. */
70 #ifdef abort
71 #undef abort
72 #endif
74 /* Some types used by the DWARF 2 spec. */
76 typedef int sword __attribute__ ((mode (SI)));
77 typedef unsigned int uword __attribute__ ((mode (SI)));
78 typedef unsigned int uaddr __attribute__ ((mode (pointer)));
79 typedef int saddr __attribute__ ((mode (pointer)));
80 typedef unsigned char ubyte;
82 /* Terminology:
83 CIE - Common Information Element
84 FDE - Frame Descriptor Element
86 There is one per function, and it describes where the function code
87 is located, and what the register lifetimes and stack layout are
88 within the function.
90 The data structures are defined in the DWARF specfication, although
91 not in a very readable way (see LITERATURE).
93 Every time an exception is thrown, the code needs to locate the FDE
94 for the current function, and starts to look for exception regions
95 from that FDE. This works in a two-level search:
96 a) in a linear search, find the shared image (i.e. DLL) containing
97 the PC
98 b) using the FDE table for that shared object, locate the FDE using
99 binary search (which requires the sorting). */
101 /* The first few fields of a CIE. The CIE_id field is 0 for a CIE,
102 to distinguish it from a valid FDE. FDEs are aligned to an addressing
103 unit boundary, but the fields within are unaligned. */
105 struct dwarf_cie {
106 uword length;
107 sword CIE_id;
108 ubyte version;
109 char augmentation[0];
110 } __attribute__ ((packed, aligned (__alignof__ (void *))));
112 /* The first few fields of an FDE. */
114 struct dwarf_fde {
115 uword length;
116 sword CIE_delta;
117 void* pc_begin;
118 uaddr pc_range;
119 } __attribute__ ((packed, aligned (__alignof__ (void *))));
121 typedef struct dwarf_fde fde;
123 /* Objects to be searched for frame unwind info. */
125 static struct object *objects;
127 /* The information we care about from a CIE. */
129 struct cie_info {
130 char *augmentation;
131 void *eh_ptr;
132 int code_align;
133 int data_align;
134 unsigned ra_regno;
137 /* The current unwind state, plus a saved copy for DW_CFA_remember_state. */
139 struct frame_state_internal
141 struct frame_state s;
142 struct frame_state_internal *saved_state;
145 /* This is undefined below if we need it to be an actual function. */
146 #define init_object_mutex_once()
148 #if __GTHREADS
149 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
151 /* Helper for init_object_mutex_once. */
153 static void
154 init_object_mutex (void)
156 __GTHREAD_MUTEX_INIT_FUNCTION (&object_mutex);
159 /* Call this to arrange to initialize the object mutex. */
161 #undef init_object_mutex_once
162 static void
163 init_object_mutex_once (void)
165 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
166 __gthread_once (&once, init_object_mutex);
169 #endif /* __GTHREAD_MUTEX_INIT_FUNCTION */
170 #endif /* __GTHREADS */
172 /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
173 by R, and return the new value of BUF. */
175 static void *
176 decode_uleb128 (unsigned char *buf, unsigned *r)
178 unsigned shift = 0;
179 unsigned result = 0;
181 while (1)
183 unsigned byte = *buf++;
184 result |= (byte & 0x7f) << shift;
185 if ((byte & 0x80) == 0)
186 break;
187 shift += 7;
189 *r = result;
190 return buf;
193 /* Decode the signed LEB128 constant at BUF into the variable pointed to
194 by R, and return the new value of BUF. */
196 static void *
197 decode_sleb128 (unsigned char *buf, int *r)
199 unsigned shift = 0;
200 unsigned result = 0;
201 unsigned byte;
203 while (1)
205 byte = *buf++;
206 result |= (byte & 0x7f) << shift;
207 shift += 7;
208 if ((byte & 0x80) == 0)
209 break;
211 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
212 result |= - (1 << shift);
214 *r = result;
215 return buf;
218 /* Read unaligned data from the instruction buffer. */
220 union unaligned {
221 void *p;
222 unsigned b2 __attribute__ ((mode (HI)));
223 unsigned b4 __attribute__ ((mode (SI)));
224 unsigned b8 __attribute__ ((mode (DI)));
225 } __attribute__ ((packed));
226 static inline void *
227 read_pointer (void *p)
228 { union unaligned *up = p; return up->p; }
229 static inline unsigned
230 read_1byte (void *p)
231 { return *(unsigned char *)p; }
232 static inline unsigned
233 read_2byte (void *p)
234 { union unaligned *up = p; return up->b2; }
235 static inline unsigned
236 read_4byte (void *p)
237 { union unaligned *up = p; return up->b4; }
238 static inline unsigned long
239 read_8byte (void *p)
240 { union unaligned *up = p; return up->b8; }
242 /* Ordering function for FDEs. Functions can't overlap, so we just compare
243 their starting addresses. */
245 static inline saddr
246 fde_compare (fde *x, fde *y)
248 return (saddr)x->pc_begin - (saddr)y->pc_begin;
251 /* Return the address of the FDE after P. */
253 static inline fde *
254 next_fde (fde *p)
256 return (fde *)(((char *)p) + p->length + sizeof (p->length));
259 /* Sorting an array of FDEs by address.
260 (Ideally we would have the linker sort the FDEs so we don't have to do
261 it at run time. But the linkers are not yet prepared for this.) */
263 /* This is a special mix of insertion sort and heap sort, optimized for
264 the data sets that actually occur. They look like
265 101 102 103 127 128 105 108 110 190 111 115 119 125 160 126 129 130.
266 I.e. a linearly increasing sequence (coming from functions in the text
267 section), with additionally a few unordered elements (coming from functions
268 in gnu_linkonce sections) whose values are higher than the values in the
269 surrounding linear sequence (but not necessarily higher than the values
270 at the end of the linear sequence!).
271 The worst-case total run time is O(N) + O(n log (n)), where N is the
272 total number of FDEs and n is the number of erratic ones. */
274 typedef struct fde_vector
276 fde **array;
277 size_t count;
278 } fde_vector;
280 typedef struct fde_accumulator
282 fde_vector linear;
283 fde_vector erratic;
284 } fde_accumulator;
286 static inline void
287 start_fde_sort (fde_accumulator *accu, size_t count)
289 accu->linear.array = (fde **) malloc (sizeof (fde *) * count);
290 accu->erratic.array = (fde **) malloc (sizeof (fde *) * count);
291 accu->linear.count = 0;
292 accu->erratic.count = 0;
295 static inline void
296 fde_insert (fde_accumulator *accu, fde *this_fde)
298 accu->linear.array[accu->linear.count++] = this_fde;
301 /* Split LINEAR into a linear sequence with low values and an erratic
302 sequence with high values, put the linear one (of longest possible
303 length) into LINEAR and the erratic one into ERRATIC. This is O(N).
305 Because the longest linear sequence we are trying to locate within the
306 incoming LINEAR array can be interspersed with (high valued) erratic
307 entries. We construct a chain indicating the sequenced entries.
308 To avoid having to allocate this chain, we overlay it onto the space of
309 the ERRATIC array during construction. A final pass iterates over the
310 chain to determine what should be placed in the ERRATIC array, and
311 what is the linear sequence. This overlay is safe from aliasing. */
312 static inline void
313 fde_split (fde_vector *linear, fde_vector *erratic)
315 static fde *marker;
316 size_t count = linear->count;
317 fde **chain_end = &marker;
318 size_t i, j, k;
320 /* This should optimize out, but it is wise to make sure this assumption
321 is correct. Should these have different sizes, we cannot cast between
322 them and the overlaying onto ERRATIC will not work. */
323 if (sizeof (fde *) != sizeof (fde **))
324 abort ();
326 for (i = 0; i < count; i++)
328 fde **probe;
330 for (probe = chain_end;
331 probe != &marker && fde_compare (linear->array[i], *probe) < 0;
332 probe = chain_end)
334 chain_end = (fde **)erratic->array[probe - linear->array];
335 erratic->array[probe - linear->array] = NULL;
337 erratic->array[i] = (fde *)chain_end;
338 chain_end = &linear->array[i];
341 /* Each entry in LINEAR which is part of the linear sequence we have
342 discovered will correspond to a non-NULL entry in the chain we built in
343 the ERRATIC array. */
344 for (i = j = k = 0; i < count; i++)
345 if (erratic->array[i])
346 linear->array[j++] = linear->array[i];
347 else
348 erratic->array[k++] = linear->array[i];
349 linear->count = j;
350 erratic->count = k;
353 /* This is O(n log(n)). BSD/OS defines heapsort in stdlib.h, so we must
354 use a name that does not conflict. */
355 static inline void
356 frame_heapsort (fde_vector *erratic)
358 /* For a description of this algorithm, see:
359 Samuel P. Harbison, Guy L. Steele Jr.: C, a reference manual, 2nd ed.,
360 p. 60-61. */
361 fde ** a = erratic->array;
362 /* A portion of the array is called a "heap" if for all i>=0:
363 If i and 2i+1 are valid indices, then a[i] >= a[2i+1].
364 If i and 2i+2 are valid indices, then a[i] >= a[2i+2]. */
365 #define SWAP(x,y) do { fde * tmp = x; x = y; y = tmp; } while (0)
366 size_t n = erratic->count;
367 size_t m = n;
368 size_t i;
370 while (m > 0)
372 /* Invariant: a[m..n-1] is a heap. */
373 m--;
374 for (i = m; 2*i+1 < n; )
376 if (2*i+2 < n
377 && fde_compare (a[2*i+2], a[2*i+1]) > 0
378 && fde_compare (a[2*i+2], a[i]) > 0)
380 SWAP (a[i], a[2*i+2]);
381 i = 2*i+2;
383 else if (fde_compare (a[2*i+1], a[i]) > 0)
385 SWAP (a[i], a[2*i+1]);
386 i = 2*i+1;
388 else
389 break;
392 while (n > 1)
394 /* Invariant: a[0..n-1] is a heap. */
395 n--;
396 SWAP (a[0], a[n]);
397 for (i = 0; 2*i+1 < n; )
399 if (2*i+2 < n
400 && fde_compare (a[2*i+2], a[2*i+1]) > 0
401 && fde_compare (a[2*i+2], a[i]) > 0)
403 SWAP (a[i], a[2*i+2]);
404 i = 2*i+2;
406 else if (fde_compare (a[2*i+1], a[i]) > 0)
408 SWAP (a[i], a[2*i+1]);
409 i = 2*i+1;
411 else
412 break;
415 #undef SWAP
418 /* Merge V1 and V2, both sorted, and put the result into V1. */
419 static void
420 fde_merge (fde_vector *v1, const fde_vector *v2)
422 size_t i1, i2;
423 fde * fde2;
425 i2 = v2->count;
426 if (i2 > 0)
428 i1 = v1->count;
429 do {
430 i2--;
431 fde2 = v2->array[i2];
432 while (i1 > 0 && fde_compare (v1->array[i1-1], fde2) > 0)
434 v1->array[i1+i2] = v1->array[i1-1];
435 i1--;
437 v1->array[i1+i2] = fde2;
438 } while (i2 > 0);
439 v1->count += v2->count;
443 static fde **
444 end_fde_sort (fde_accumulator *accu, size_t count)
446 if (accu->linear.count != count)
447 abort ();
448 fde_split (&accu->linear, &accu->erratic);
449 if (accu->linear.count + accu->erratic.count != count)
450 abort ();
451 frame_heapsort (&accu->erratic);
452 fde_merge (&accu->linear, &accu->erratic);
453 free (accu->erratic.array);
454 return accu->linear.array;
457 static size_t
458 count_fdes (fde *this_fde)
460 size_t count;
462 for (count = 0; this_fde->length != 0; this_fde = next_fde (this_fde))
464 /* Skip CIEs and linked once FDE entries. */
465 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
466 continue;
468 ++count;
471 return count;
474 static void
475 add_fdes (fde *this_fde, fde_accumulator *accu, void **beg_ptr, void **end_ptr)
477 void *pc_begin = *beg_ptr;
478 void *pc_end = *end_ptr;
480 for (; this_fde->length != 0; this_fde = next_fde (this_fde))
482 /* Skip CIEs and linked once FDE entries. */
483 if (this_fde->CIE_delta == 0 || this_fde->pc_begin == 0)
484 continue;
486 fde_insert (accu, this_fde);
488 if (this_fde->pc_begin < pc_begin)
489 pc_begin = this_fde->pc_begin;
490 if (this_fde->pc_begin + this_fde->pc_range > pc_end)
491 pc_end = this_fde->pc_begin + this_fde->pc_range;
494 *beg_ptr = pc_begin;
495 *end_ptr = pc_end;
498 /* Set up a sorted array of pointers to FDEs for a loaded object. We
499 count up the entries before allocating the array because it's likely to
500 be faster. */
502 static void
503 frame_init (struct object* ob)
505 size_t count;
506 fde_accumulator accu;
507 void *pc_begin, *pc_end;
509 if (ob->fde_array)
511 fde **p = ob->fde_array;
512 for (count = 0; *p; ++p)
513 count += count_fdes (*p);
515 else
516 count = count_fdes (ob->fde_begin);
518 ob->count = count;
520 start_fde_sort (&accu, count);
521 pc_begin = (void*)(uaddr)-1;
522 pc_end = 0;
524 if (ob->fde_array)
526 fde **p = ob->fde_array;
527 for (; *p; ++p)
528 add_fdes (*p, &accu, &pc_begin, &pc_end);
530 else
531 add_fdes (ob->fde_begin, &accu, &pc_begin, &pc_end);
533 ob->fde_array = end_fde_sort (&accu, count);
534 ob->pc_begin = pc_begin;
535 ob->pc_end = pc_end;
538 /* Return a pointer to the FDE for the function containing PC. */
540 static fde *
541 find_fde (void *pc)
543 struct object *ob;
544 size_t lo, hi;
546 init_object_mutex_once ();
547 __gthread_mutex_lock (&object_mutex);
549 for (ob = objects; ob; ob = ob->next)
551 if (ob->pc_begin == 0)
552 frame_init (ob);
553 if (pc >= ob->pc_begin && pc < ob->pc_end)
554 break;
557 __gthread_mutex_unlock (&object_mutex);
559 if (ob == 0)
560 return 0;
562 /* Standard binary search algorithm. */
563 for (lo = 0, hi = ob->count; lo < hi; )
565 size_t i = (lo + hi) / 2;
566 fde *f = ob->fde_array[i];
568 if (pc < f->pc_begin)
569 hi = i;
570 else if (pc >= f->pc_begin + f->pc_range)
571 lo = i + 1;
572 else
573 return f;
576 return 0;
579 static inline struct dwarf_cie *
580 get_cie (fde *f)
582 return ((void *)&f->CIE_delta) - f->CIE_delta;
585 /* Extract any interesting information from the CIE for the translation
586 unit F belongs to. */
588 static void *
589 extract_cie_info (fde *f, struct cie_info *c)
591 void *p;
592 int i;
594 c->augmentation = get_cie (f)->augmentation;
596 if (strcmp (c->augmentation, "") != 0
597 && strcmp (c->augmentation, "eh") != 0
598 && c->augmentation[0] != 'z')
599 return 0;
601 p = c->augmentation + strlen (c->augmentation) + 1;
603 if (strcmp (c->augmentation, "eh") == 0)
605 c->eh_ptr = read_pointer (p);
606 p += sizeof (void *);
608 else
609 c->eh_ptr = 0;
611 p = decode_uleb128 (p, &c->code_align);
612 p = decode_sleb128 (p, &c->data_align);
613 c->ra_regno = *(unsigned char *)p++;
615 /* If the augmentation starts with 'z', we now see the length of the
616 augmentation fields. */
617 if (c->augmentation[0] == 'z')
619 p = decode_uleb128 (p, &i);
620 p += i;
623 return p;
626 /* Decode one instruction's worth of DWARF 2 call frame information.
627 Used by __frame_state_for. Takes pointers P to the instruction to
628 decode, STATE to the current register unwind information, INFO to the
629 current CIE information, and PC to the current PC value. Returns a
630 pointer to the next instruction. */
632 static void *
633 execute_cfa_insn (void *p, struct frame_state_internal *state,
634 struct cie_info *info, void **pc)
636 unsigned insn = *(unsigned char *)p++;
637 unsigned reg;
638 int offset;
640 if (insn & DW_CFA_advance_loc)
641 *pc += ((insn & 0x3f) * info->code_align);
642 else if (insn & DW_CFA_offset)
644 reg = (insn & 0x3f);
645 p = decode_uleb128 (p, &offset);
646 offset *= info->data_align;
647 state->s.saved[reg] = REG_SAVED_OFFSET;
648 state->s.reg_or_offset[reg] = offset;
650 else if (insn & DW_CFA_restore)
652 reg = (insn & 0x3f);
653 state->s.saved[reg] = REG_UNSAVED;
655 else switch (insn)
657 case DW_CFA_set_loc:
658 *pc = read_pointer (p);
659 p += sizeof (void *);
660 break;
661 case DW_CFA_advance_loc1:
662 *pc += read_1byte (p);
663 p += 1;
664 break;
665 case DW_CFA_advance_loc2:
666 *pc += read_2byte (p);
667 p += 2;
668 break;
669 case DW_CFA_advance_loc4:
670 *pc += read_4byte (p);
671 p += 4;
672 break;
674 case DW_CFA_offset_extended:
675 p = decode_uleb128 (p, &reg);
676 p = decode_uleb128 (p, &offset);
677 offset *= info->data_align;
678 state->s.saved[reg] = REG_SAVED_OFFSET;
679 state->s.reg_or_offset[reg] = offset;
680 break;
681 case DW_CFA_restore_extended:
682 p = decode_uleb128 (p, &reg);
683 state->s.saved[reg] = REG_UNSAVED;
684 break;
686 case DW_CFA_undefined:
687 case DW_CFA_same_value:
688 case DW_CFA_nop:
689 break;
691 case DW_CFA_register:
693 unsigned reg2;
694 p = decode_uleb128 (p, &reg);
695 p = decode_uleb128 (p, &reg2);
696 state->s.saved[reg] = REG_SAVED_REG;
697 state->s.reg_or_offset[reg] = reg2;
699 break;
701 case DW_CFA_def_cfa:
702 p = decode_uleb128 (p, &reg);
703 p = decode_uleb128 (p, &offset);
704 state->s.cfa_reg = reg;
705 state->s.cfa_offset = offset;
706 break;
707 case DW_CFA_def_cfa_register:
708 p = decode_uleb128 (p, &reg);
709 state->s.cfa_reg = reg;
710 break;
711 case DW_CFA_def_cfa_offset:
712 p = decode_uleb128 (p, &offset);
713 state->s.cfa_offset = offset;
714 break;
716 case DW_CFA_remember_state:
718 struct frame_state_internal *save =
719 (struct frame_state_internal *)
720 malloc (sizeof (struct frame_state_internal));
721 memcpy (save, state, sizeof (struct frame_state_internal));
722 state->saved_state = save;
724 break;
725 case DW_CFA_restore_state:
727 struct frame_state_internal *save = state->saved_state;
728 memcpy (state, save, sizeof (struct frame_state_internal));
729 free (save);
731 break;
733 /* FIXME: Hardcoded for SPARC register window configuration. */
734 case DW_CFA_GNU_window_save:
735 for (reg = 16; reg < 32; ++reg)
737 state->s.saved[reg] = REG_SAVED_OFFSET;
738 state->s.reg_or_offset[reg] = (reg - 16) * sizeof (void *);
740 break;
742 case DW_CFA_GNU_args_size:
743 p = decode_uleb128 (p, &offset);
744 state->s.args_size = offset;
745 break;
747 default:
748 abort ();
750 return p;
753 /* Called from crtbegin.o to register the unwind info for an object. */
755 void
756 __register_frame_info (void *begin, struct object *ob)
758 ob->fde_begin = begin;
760 ob->pc_begin = ob->pc_end = 0;
761 ob->fde_array = 0;
762 ob->count = 0;
764 init_object_mutex_once ();
765 __gthread_mutex_lock (&object_mutex);
767 ob->next = objects;
768 objects = ob;
770 __gthread_mutex_unlock (&object_mutex);
773 void
774 __register_frame (void *begin)
776 struct object *ob = (struct object *) malloc (sizeof (struct object));
777 __register_frame_info (begin, ob);
780 /* Similar, but BEGIN is actually a pointer to a table of unwind entries
781 for different translation units. Called from the file generated by
782 collect2. */
784 void
785 __register_frame_info_table (void *begin, struct object *ob)
787 ob->fde_begin = begin;
788 ob->fde_array = begin;
790 ob->pc_begin = ob->pc_end = 0;
791 ob->count = 0;
793 init_object_mutex_once ();
794 __gthread_mutex_lock (&object_mutex);
796 ob->next = objects;
797 objects = ob;
799 __gthread_mutex_unlock (&object_mutex);
802 void
803 __register_frame_table (void *begin)
805 struct object *ob = (struct object *) malloc (sizeof (struct object));
806 __register_frame_info_table (begin, ob);
809 /* Called from crtbegin.o to deregister the unwind info for an object. */
811 void *
812 __deregister_frame_info (void *begin)
814 struct object **p;
816 init_object_mutex_once ();
817 __gthread_mutex_lock (&object_mutex);
819 p = &objects;
820 while (*p)
822 if ((*p)->fde_begin == begin)
824 struct object *ob = *p;
825 *p = (*p)->next;
827 /* If we've run init_frame for this object, free the FDE array. */
828 if (ob->pc_begin)
829 free (ob->fde_array);
831 __gthread_mutex_unlock (&object_mutex);
832 return (void *) ob;
834 p = &((*p)->next);
837 __gthread_mutex_unlock (&object_mutex);
838 abort ();
841 void
842 __deregister_frame (void *begin)
844 free (__deregister_frame_info (begin));
847 /* Called from __throw to find the registers to restore for a given
848 PC_TARGET. The caller should allocate a local variable of `struct
849 frame_state' (declared in frame.h) and pass its address to STATE_IN. */
851 struct frame_state *
852 __frame_state_for (void *pc_target, struct frame_state *state_in)
854 fde *f;
855 void *insn, *end, *pc;
856 struct cie_info info;
857 struct frame_state_internal state;
859 f = find_fde (pc_target);
860 if (f == 0)
861 return 0;
863 insn = extract_cie_info (f, &info);
864 if (insn == 0)
865 return 0;
867 memset (&state, 0, sizeof (state));
868 state.s.retaddr_column = info.ra_regno;
869 state.s.eh_ptr = info.eh_ptr;
871 /* First decode all the insns in the CIE. */
872 end = next_fde ((fde*) get_cie (f));
873 while (insn < end)
874 insn = execute_cfa_insn (insn, &state, &info, 0);
876 insn = ((fde *)f) + 1;
878 if (info.augmentation[0] == 'z')
880 int i;
881 insn = decode_uleb128 (insn, &i);
882 insn += i;
885 /* Then the insns in the FDE up to our target PC. */
886 end = next_fde (f);
887 pc = f->pc_begin;
888 while (insn < end && pc <= pc_target)
889 insn = execute_cfa_insn (insn, &state, &info, &pc);
891 memcpy (state_in, &state.s, sizeof (state.s));
892 return state_in;
894 #endif /* DWARF2_UNWIND_INFO */