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