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