unwind-dw2.c revision 1.1 1 /* DWARF2 exception handling and frame unwind runtime interface routines.
2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "tconfig.h"
26 #include "tsystem.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "libgcc_tm.h"
30 #include "dwarf2.h"
31 #include "unwind.h"
32 #ifdef __USING_SJLJ_EXCEPTIONS__
33 # define NO_SIZE_OF_ENCODED_VALUE
34 #endif
35 #include "unwind-pe.h"
36 #include "unwind-dw2-fde.h"
37 #include "gthr.h"
38 #include "unwind-dw2.h"
39
40 #ifdef HAVE_SYS_SDT_H
41 #include <sys/sdt.h>
42 #endif
43
44 #ifndef __USING_SJLJ_EXCEPTIONS__
45
46 #ifndef STACK_GROWS_DOWNWARD
47 #define STACK_GROWS_DOWNWARD 0
48 #else
49 #undef STACK_GROWS_DOWNWARD
50 #define STACK_GROWS_DOWNWARD 1
51 #endif
52
53 /* Dwarf frame registers used for pre gcc 3.0 compiled glibc. */
54 #ifndef PRE_GCC3_DWARF_FRAME_REGISTERS
55 #define PRE_GCC3_DWARF_FRAME_REGISTERS DWARF_FRAME_REGISTERS
56 #endif
57
58 #ifndef DWARF_REG_TO_UNWIND_COLUMN
59 #define DWARF_REG_TO_UNWIND_COLUMN(REGNO) (REGNO)
60 #endif
61
62 /* ??? For the public function interfaces, we tend to gcc_assert that the
63 column numbers are in range. For the dwarf2 unwind info this does happen,
64 although so far in a case that doesn't actually matter.
65
66 See PR49146, in which a call from x86_64 ms abi to x86_64 unix abi stores
67 the call-saved xmm registers and annotates them. We havn't bothered
68 providing support for the xmm registers for the x86_64 port primarily
69 because the 64-bit windows targets don't use dwarf2 unwind, using sjlj or
70 SEH instead. Adding the support for unix targets would generally be a
71 waste. However, some runtime libraries supplied with ICC do contain such
72 an unorthodox transition, as well as the unwind info to match. This loss
73 of register restoration doesn't matter in practice, because the exception
74 is caught in the native unix abi, where all of the xmm registers are
75 call clobbered.
76
77 Ideally, we'd record some bit to notice when we're failing to restore some
78 register recorded in the unwind info, but to do that we need annotation on
79 the unix->ms abi edge, so that we know when the register data may be
80 discarded. And since this edge is also within the ICC library, we're
81 unlikely to be able to get the new annotation.
82
83 Barring a magic solution to restore the ms abi defined 128-bit xmm registers
84 (as distictly opposed to the full runtime width) without causing extra
85 overhead for normal unix abis, the best solution seems to be to simply
86 ignore unwind data for unknown columns. */
87
88 #define UNWIND_COLUMN_IN_RANGE(x) \
89 __builtin_expect((x) <= DWARF_FRAME_REGISTERS, 1)
90
91 #ifdef REG_VALUE_IN_UNWIND_CONTEXT
92 typedef _Unwind_Word _Unwind_Context_Reg_Val;
93
94 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
95 #define ASSUME_EXTENDED_UNWIND_CONTEXT 1
96 #endif
97
98 static inline _Unwind_Word
99 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
100 {
101 return val;
102 }
103
104 static inline _Unwind_Context_Reg_Val
105 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
106 {
107 return val;
108 }
109 #else
110 typedef void *_Unwind_Context_Reg_Val;
111
112 static inline _Unwind_Word
113 _Unwind_Get_Unwind_Word (_Unwind_Context_Reg_Val val)
114 {
115 return (_Unwind_Word) (_Unwind_Internal_Ptr) val;
116 }
117
118 static inline _Unwind_Context_Reg_Val
119 _Unwind_Get_Unwind_Context_Reg_Val (_Unwind_Word val)
120 {
121 return (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) val;
122 }
123 #endif
124
125 #ifndef ASSUME_EXTENDED_UNWIND_CONTEXT
126 #define ASSUME_EXTENDED_UNWIND_CONTEXT 0
127 #endif
128
129 /* This is the register and unwind state for a particular frame. This
130 provides the information necessary to unwind up past a frame and return
131 to its caller. */
132 struct _Unwind_Context
133 {
134 _Unwind_Context_Reg_Val reg[DWARF_FRAME_REGISTERS+1];
135 void *cfa;
136 void *ra;
137 void *lsda;
138 struct dwarf_eh_bases bases;
139 /* Signal frame context. */
140 #define SIGNAL_FRAME_BIT ((~(_Unwind_Word) 0 >> 1) + 1)
141 /* Context which has version/args_size/by_value fields. */
142 #define EXTENDED_CONTEXT_BIT ((~(_Unwind_Word) 0 >> 2) + 1)
143 _Unwind_Word flags;
144 /* 0 for now, can be increased when further fields are added to
145 struct _Unwind_Context. */
146 _Unwind_Word version;
147 _Unwind_Word args_size;
148 char by_value[DWARF_FRAME_REGISTERS+1];
149 };
150
151 /* Byte size of every register managed by these routines. */
152 static unsigned char dwarf_reg_size_table[DWARF_FRAME_REGISTERS+1];
153
154
155 /* Read unaligned data from the instruction buffer. */
157
158 union unaligned
159 {
160 void *p;
161 unsigned u2 __attribute__ ((mode (HI)));
162 unsigned u4 __attribute__ ((mode (SI)));
163 unsigned u8 __attribute__ ((mode (DI)));
164 signed s2 __attribute__ ((mode (HI)));
165 signed s4 __attribute__ ((mode (SI)));
166 signed s8 __attribute__ ((mode (DI)));
167 } __attribute__ ((packed));
168
169 static void uw_update_context (struct _Unwind_Context *, _Unwind_FrameState *);
170 static _Unwind_Reason_Code uw_frame_state_for (struct _Unwind_Context *,
171 _Unwind_FrameState *);
172
173 static inline void *
174 read_pointer (const void *p) { const union unaligned *up = p; return up->p; }
175
176 static inline int
177 read_1u (const void *p) { return *(const unsigned char *) p; }
178
179 static inline int
180 read_1s (const void *p) { return *(const signed char *) p; }
181
182 static inline int
183 read_2u (const void *p) { const union unaligned *up = p; return up->u2; }
184
185 static inline int
186 read_2s (const void *p) { const union unaligned *up = p; return up->s2; }
187
188 static inline unsigned int
189 read_4u (const void *p) { const union unaligned *up = p; return up->u4; }
190
191 static inline int
192 read_4s (const void *p) { const union unaligned *up = p; return up->s4; }
193
194 static inline unsigned long
195 read_8u (const void *p) { const union unaligned *up = p; return up->u8; }
196
197 static inline unsigned long
198 read_8s (const void *p) { const union unaligned *up = p; return up->s8; }
199
200 static inline _Unwind_Word
202 _Unwind_IsSignalFrame (struct _Unwind_Context *context)
203 {
204 return (context->flags & SIGNAL_FRAME_BIT) ? 1 : 0;
205 }
206
207 static inline void
208 _Unwind_SetSignalFrame (struct _Unwind_Context *context, int val)
209 {
210 if (val)
211 context->flags |= SIGNAL_FRAME_BIT;
212 else
213 context->flags &= ~SIGNAL_FRAME_BIT;
214 }
215
216 static inline _Unwind_Word
217 _Unwind_IsExtendedContext (struct _Unwind_Context *context)
218 {
219 return (ASSUME_EXTENDED_UNWIND_CONTEXT
220 || (context->flags & EXTENDED_CONTEXT_BIT));
221 }
222
223 /* Get the value of register INDEX as saved in CONTEXT. */
225
226 inline _Unwind_Word
227 _Unwind_GetGR (struct _Unwind_Context *context, int index)
228 {
229 int size;
230 _Unwind_Context_Reg_Val val;
231
232 #ifdef DWARF_ZERO_REG
233 if (index == DWARF_ZERO_REG)
234 return 0;
235 #endif
236
237 index = DWARF_REG_TO_UNWIND_COLUMN (index);
238 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
239 size = dwarf_reg_size_table[index];
240 val = context->reg[index];
241
242 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
243 return _Unwind_Get_Unwind_Word (val);
244
245 /* This will segfault if the register hasn't been saved. */
246 if (size == sizeof(_Unwind_Ptr))
247 return * (_Unwind_Ptr *) (_Unwind_Internal_Ptr) val;
248 else
249 {
250 gcc_assert (size == sizeof(_Unwind_Word));
251 return * (_Unwind_Word *) (_Unwind_Internal_Ptr) val;
252 }
253 }
254
255 static inline void *
256 _Unwind_GetPtr (struct _Unwind_Context *context, int index)
257 {
258 return (void *)(_Unwind_Ptr) _Unwind_GetGR (context, index);
259 }
260
261 /* Get the value of the CFA as saved in CONTEXT. */
262
263 _Unwind_Word
264 _Unwind_GetCFA (struct _Unwind_Context *context)
265 {
266 return (_Unwind_Ptr) context->cfa;
267 }
268
269 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
270
271 inline void
272 _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val)
273 {
274 int size;
275 void *ptr;
276
277 index = DWARF_REG_TO_UNWIND_COLUMN (index);
278 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
279 size = dwarf_reg_size_table[index];
280
281 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
282 {
283 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
284 return;
285 }
286
287 ptr = (void *) (_Unwind_Internal_Ptr) context->reg[index];
288
289 if (size == sizeof(_Unwind_Ptr))
290 * (_Unwind_Ptr *) ptr = val;
291 else
292 {
293 gcc_assert (size == sizeof(_Unwind_Word));
294 * (_Unwind_Word *) ptr = val;
295 }
296 }
297
298 /* Get the pointer to a register INDEX as saved in CONTEXT. */
299
300 static inline void *
301 _Unwind_GetGRPtr (struct _Unwind_Context *context, int index)
302 {
303 index = DWARF_REG_TO_UNWIND_COLUMN (index);
304 if (_Unwind_IsExtendedContext (context) && context->by_value[index])
305 return &context->reg[index];
306 return (void *) (_Unwind_Internal_Ptr) context->reg[index];
307 }
308
309 /* Set the pointer to a register INDEX as saved in CONTEXT. */
310
311 static inline void
312 _Unwind_SetGRPtr (struct _Unwind_Context *context, int index, void *p)
313 {
314 index = DWARF_REG_TO_UNWIND_COLUMN (index);
315 if (_Unwind_IsExtendedContext (context))
316 context->by_value[index] = 0;
317 context->reg[index] = (_Unwind_Context_Reg_Val) (_Unwind_Internal_Ptr) p;
318 }
319
320 /* Overwrite the saved value for register INDEX in CONTEXT with VAL. */
321
322 static inline void
323 _Unwind_SetGRValue (struct _Unwind_Context *context, int index,
324 _Unwind_Word val)
325 {
326 index = DWARF_REG_TO_UNWIND_COLUMN (index);
327 gcc_assert (index < (int) sizeof(dwarf_reg_size_table));
328 /* Return column size may be smaller than _Unwind_Context_Reg_Val. */
329 gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val));
330
331 context->by_value[index] = 1;
332 context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val);
333 }
334
335 /* Return nonzero if register INDEX is stored by value rather than
336 by reference. */
337
338 static inline int
339 _Unwind_GRByValue (struct _Unwind_Context *context, int index)
340 {
341 index = DWARF_REG_TO_UNWIND_COLUMN (index);
342 return context->by_value[index];
343 }
344
345 /* Retrieve the return address for CONTEXT. */
346
347 inline _Unwind_Ptr
348 _Unwind_GetIP (struct _Unwind_Context *context)
349 {
350 return (_Unwind_Ptr) context->ra;
351 }
352
353 /* Retrieve the return address and flag whether that IP is before
354 or after first not yet fully executed instruction. */
355
356 inline _Unwind_Ptr
357 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
358 {
359 *ip_before_insn = _Unwind_IsSignalFrame (context);
360 return (_Unwind_Ptr) context->ra;
361 }
362
363 /* Overwrite the return address for CONTEXT with VAL. */
364
365 inline void
366 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
367 {
368 context->ra = (void *) val;
369 }
370
371 void *
372 _Unwind_GetLanguageSpecificData (struct _Unwind_Context *context)
373 {
374 return context->lsda;
375 }
376
377 _Unwind_Ptr
378 _Unwind_GetRegionStart (struct _Unwind_Context *context)
379 {
380 return (_Unwind_Ptr) context->bases.func;
381 }
382
383 void *
384 _Unwind_FindEnclosingFunction (void *pc)
385 {
386 struct dwarf_eh_bases bases;
387 const struct dwarf_fde *fde = _Unwind_Find_FDE (pc-1, &bases);
388 if (fde)
389 return bases.func;
390 else
391 return NULL;
392 }
393
394 #ifndef __ia64__
395 _Unwind_Ptr
396 _Unwind_GetDataRelBase (struct _Unwind_Context *context)
397 {
398 return (_Unwind_Ptr) context->bases.dbase;
399 }
400
401 _Unwind_Ptr
402 _Unwind_GetTextRelBase (struct _Unwind_Context *context)
403 {
404 return (_Unwind_Ptr) context->bases.tbase;
405 }
406 #endif
407
408 #include "md-unwind-support.h"
409
410 /* Extract any interesting information from the CIE for the translation
412 unit F belongs to. Return a pointer to the byte after the augmentation,
413 or NULL if we encountered an undecipherable augmentation. */
414
415 static const unsigned char *
416 extract_cie_info (const struct dwarf_cie *cie, struct _Unwind_Context *context,
417 _Unwind_FrameState *fs)
418 {
419 const unsigned char *aug = cie->augmentation;
420 const unsigned char *p = aug + strlen ((const char *)aug) + 1;
421 const unsigned char *ret = NULL;
422 _uleb128_t utmp;
423 _sleb128_t stmp;
424
425 /* g++ v2 "eh" has pointer immediately following augmentation string,
426 so it must be handled first. */
427 if (aug[0] == 'e' && aug[1] == 'h')
428 {
429 fs->eh_ptr = read_pointer (p);
430 p += sizeof (void *);
431 aug += 2;
432 }
433
434 /* After the augmentation resp. pointer for "eh" augmentation
435 follows for CIE version >= 4 address size byte and
436 segment size byte. */
437 if (__builtin_expect (cie->version >= 4, 0))
438 {
439 if (p[0] != sizeof (void *) || p[1] != 0)
440 return NULL;
441 p += 2;
442 }
443 /* Immediately following this are the code and
444 data alignment and return address column. */
445 p = read_uleb128 (p, &utmp);
446 fs->code_align = (_Unwind_Word)utmp;
447 p = read_sleb128 (p, &stmp);
448 fs->data_align = (_Unwind_Sword)stmp;
449 if (cie->version == 1)
450 fs->retaddr_column = *p++;
451 else
452 {
453 p = read_uleb128 (p, &utmp);
454 fs->retaddr_column = (_Unwind_Word)utmp;
455 }
456 fs->lsda_encoding = DW_EH_PE_omit;
457
458 /* If the augmentation starts with 'z', then a uleb128 immediately
459 follows containing the length of the augmentation field following
460 the size. */
461 if (*aug == 'z')
462 {
463 p = read_uleb128 (p, &utmp);
464 ret = p + utmp;
465
466 fs->saw_z = 1;
467 ++aug;
468 }
469
470 /* Iterate over recognized augmentation subsequences. */
471 while (*aug != '\0')
472 {
473 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
474 if (aug[0] == 'L')
475 {
476 fs->lsda_encoding = *p++;
477 aug += 1;
478 }
479
480 /* "R" indicates a byte indicating how FDE addresses are encoded. */
481 else if (aug[0] == 'R')
482 {
483 fs->fde_encoding = *p++;
484 aug += 1;
485 }
486
487 /* "P" indicates a personality routine in the CIE augmentation. */
488 else if (aug[0] == 'P')
489 {
490 _Unwind_Ptr personality;
491
492 p = read_encoded_value (context, *p, p + 1, &personality);
493 fs->personality = (_Unwind_Personality_Fn) personality;
494 aug += 1;
495 }
496
497 /* "S" indicates a signal frame. */
498 else if (aug[0] == 'S')
499 {
500 fs->signal_frame = 1;
501 aug += 1;
502 }
503
504 /* Otherwise we have an unknown augmentation string.
505 Bail unless we saw a 'z' prefix. */
506 else
507 return ret;
508 }
509
510 return ret ? ret : p;
511 }
512
513
514 /* Decode a DW_OP stack program. Return the top of stack. Push INITIAL
515 onto the stack to start. */
516
517 static _Unwind_Word
518 execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end,
519 struct _Unwind_Context *context, _Unwind_Word initial)
520 {
521 _Unwind_Word stack[64]; /* ??? Assume this is enough. */
522 int stack_elt;
523
524 stack[0] = initial;
525 stack_elt = 1;
526
527 while (op_ptr < op_end)
528 {
529 enum dwarf_location_atom op = *op_ptr++;
530 _Unwind_Word result;
531 _uleb128_t reg, utmp;
532 _sleb128_t offset, stmp;
533
534 switch (op)
535 {
536 case DW_OP_lit0:
537 case DW_OP_lit1:
538 case DW_OP_lit2:
539 case DW_OP_lit3:
540 case DW_OP_lit4:
541 case DW_OP_lit5:
542 case DW_OP_lit6:
543 case DW_OP_lit7:
544 case DW_OP_lit8:
545 case DW_OP_lit9:
546 case DW_OP_lit10:
547 case DW_OP_lit11:
548 case DW_OP_lit12:
549 case DW_OP_lit13:
550 case DW_OP_lit14:
551 case DW_OP_lit15:
552 case DW_OP_lit16:
553 case DW_OP_lit17:
554 case DW_OP_lit18:
555 case DW_OP_lit19:
556 case DW_OP_lit20:
557 case DW_OP_lit21:
558 case DW_OP_lit22:
559 case DW_OP_lit23:
560 case DW_OP_lit24:
561 case DW_OP_lit25:
562 case DW_OP_lit26:
563 case DW_OP_lit27:
564 case DW_OP_lit28:
565 case DW_OP_lit29:
566 case DW_OP_lit30:
567 case DW_OP_lit31:
568 result = op - DW_OP_lit0;
569 break;
570
571 case DW_OP_addr:
572 result = (_Unwind_Word) (_Unwind_Ptr) read_pointer (op_ptr);
573 op_ptr += sizeof (void *);
574 break;
575
576 case DW_OP_GNU_encoded_addr:
577 {
578 _Unwind_Ptr presult;
579 op_ptr = read_encoded_value (context, *op_ptr, op_ptr+1, &presult);
580 result = presult;
581 }
582 break;
583
584 case DW_OP_const1u:
585 result = read_1u (op_ptr);
586 op_ptr += 1;
587 break;
588 case DW_OP_const1s:
589 result = read_1s (op_ptr);
590 op_ptr += 1;
591 break;
592 case DW_OP_const2u:
593 result = read_2u (op_ptr);
594 op_ptr += 2;
595 break;
596 case DW_OP_const2s:
597 result = read_2s (op_ptr);
598 op_ptr += 2;
599 break;
600 case DW_OP_const4u:
601 result = read_4u (op_ptr);
602 op_ptr += 4;
603 break;
604 case DW_OP_const4s:
605 result = read_4s (op_ptr);
606 op_ptr += 4;
607 break;
608 case DW_OP_const8u:
609 result = read_8u (op_ptr);
610 op_ptr += 8;
611 break;
612 case DW_OP_const8s:
613 result = read_8s (op_ptr);
614 op_ptr += 8;
615 break;
616 case DW_OP_constu:
617 op_ptr = read_uleb128 (op_ptr, &utmp);
618 result = (_Unwind_Word)utmp;
619 break;
620 case DW_OP_consts:
621 op_ptr = read_sleb128 (op_ptr, &stmp);
622 result = (_Unwind_Sword)stmp;
623 break;
624
625 case DW_OP_reg0:
626 case DW_OP_reg1:
627 case DW_OP_reg2:
628 case DW_OP_reg3:
629 case DW_OP_reg4:
630 case DW_OP_reg5:
631 case DW_OP_reg6:
632 case DW_OP_reg7:
633 case DW_OP_reg8:
634 case DW_OP_reg9:
635 case DW_OP_reg10:
636 case DW_OP_reg11:
637 case DW_OP_reg12:
638 case DW_OP_reg13:
639 case DW_OP_reg14:
640 case DW_OP_reg15:
641 case DW_OP_reg16:
642 case DW_OP_reg17:
643 case DW_OP_reg18:
644 case DW_OP_reg19:
645 case DW_OP_reg20:
646 case DW_OP_reg21:
647 case DW_OP_reg22:
648 case DW_OP_reg23:
649 case DW_OP_reg24:
650 case DW_OP_reg25:
651 case DW_OP_reg26:
652 case DW_OP_reg27:
653 case DW_OP_reg28:
654 case DW_OP_reg29:
655 case DW_OP_reg30:
656 case DW_OP_reg31:
657 result = _Unwind_GetGR (context, op - DW_OP_reg0);
658 break;
659 case DW_OP_regx:
660 op_ptr = read_uleb128 (op_ptr, ®);
661 result = _Unwind_GetGR (context, reg);
662 break;
663
664 case DW_OP_breg0:
665 case DW_OP_breg1:
666 case DW_OP_breg2:
667 case DW_OP_breg3:
668 case DW_OP_breg4:
669 case DW_OP_breg5:
670 case DW_OP_breg6:
671 case DW_OP_breg7:
672 case DW_OP_breg8:
673 case DW_OP_breg9:
674 case DW_OP_breg10:
675 case DW_OP_breg11:
676 case DW_OP_breg12:
677 case DW_OP_breg13:
678 case DW_OP_breg14:
679 case DW_OP_breg15:
680 case DW_OP_breg16:
681 case DW_OP_breg17:
682 case DW_OP_breg18:
683 case DW_OP_breg19:
684 case DW_OP_breg20:
685 case DW_OP_breg21:
686 case DW_OP_breg22:
687 case DW_OP_breg23:
688 case DW_OP_breg24:
689 case DW_OP_breg25:
690 case DW_OP_breg26:
691 case DW_OP_breg27:
692 case DW_OP_breg28:
693 case DW_OP_breg29:
694 case DW_OP_breg30:
695 case DW_OP_breg31:
696 op_ptr = read_sleb128 (op_ptr, &offset);
697 result = _Unwind_GetGR (context, op - DW_OP_breg0) + offset;
698 break;
699 case DW_OP_bregx:
700 op_ptr = read_uleb128 (op_ptr, ®);
701 op_ptr = read_sleb128 (op_ptr, &offset);
702 result = _Unwind_GetGR (context, reg) + (_Unwind_Word)offset;
703 break;
704
705 case DW_OP_dup:
706 gcc_assert (stack_elt);
707 result = stack[stack_elt - 1];
708 break;
709
710 case DW_OP_drop:
711 gcc_assert (stack_elt);
712 stack_elt -= 1;
713 goto no_push;
714
715 case DW_OP_pick:
716 offset = *op_ptr++;
717 gcc_assert (offset < stack_elt - 1);
718 result = stack[stack_elt - 1 - offset];
719 break;
720
721 case DW_OP_over:
722 gcc_assert (stack_elt >= 2);
723 result = stack[stack_elt - 2];
724 break;
725
726 case DW_OP_swap:
727 {
728 _Unwind_Word t;
729 gcc_assert (stack_elt >= 2);
730 t = stack[stack_elt - 1];
731 stack[stack_elt - 1] = stack[stack_elt - 2];
732 stack[stack_elt - 2] = t;
733 goto no_push;
734 }
735
736 case DW_OP_rot:
737 {
738 _Unwind_Word t1, t2, t3;
739
740 gcc_assert (stack_elt >= 3);
741 t1 = stack[stack_elt - 1];
742 t2 = stack[stack_elt - 2];
743 t3 = stack[stack_elt - 3];
744 stack[stack_elt - 1] = t2;
745 stack[stack_elt - 2] = t3;
746 stack[stack_elt - 3] = t1;
747 goto no_push;
748 }
749
750 case DW_OP_deref:
751 case DW_OP_deref_size:
752 case DW_OP_abs:
753 case DW_OP_neg:
754 case DW_OP_not:
755 case DW_OP_plus_uconst:
756 /* Unary operations. */
757 gcc_assert (stack_elt);
758 stack_elt -= 1;
759
760 result = stack[stack_elt];
761
762 switch (op)
763 {
764 case DW_OP_deref:
765 {
766 void *ptr = (void *) (_Unwind_Ptr) result;
767 result = (_Unwind_Ptr) read_pointer (ptr);
768 }
769 break;
770
771 case DW_OP_deref_size:
772 {
773 void *ptr = (void *) (_Unwind_Ptr) result;
774 switch (*op_ptr++)
775 {
776 case 1:
777 result = read_1u (ptr);
778 break;
779 case 2:
780 result = read_2u (ptr);
781 break;
782 case 4:
783 result = read_4u (ptr);
784 break;
785 case 8:
786 result = read_8u (ptr);
787 break;
788 default:
789 gcc_unreachable ();
790 }
791 }
792 break;
793
794 case DW_OP_abs:
795 if ((_Unwind_Sword) result < 0)
796 result = -result;
797 break;
798 case DW_OP_neg:
799 result = -result;
800 break;
801 case DW_OP_not:
802 result = ~result;
803 break;
804 case DW_OP_plus_uconst:
805 op_ptr = read_uleb128 (op_ptr, &utmp);
806 result += (_Unwind_Word)utmp;
807 break;
808
809 default:
810 gcc_unreachable ();
811 }
812 break;
813
814 case DW_OP_and:
815 case DW_OP_div:
816 case DW_OP_minus:
817 case DW_OP_mod:
818 case DW_OP_mul:
819 case DW_OP_or:
820 case DW_OP_plus:
821 case DW_OP_shl:
822 case DW_OP_shr:
823 case DW_OP_shra:
824 case DW_OP_xor:
825 case DW_OP_le:
826 case DW_OP_ge:
827 case DW_OP_eq:
828 case DW_OP_lt:
829 case DW_OP_gt:
830 case DW_OP_ne:
831 {
832 /* Binary operations. */
833 _Unwind_Word first, second;
834 gcc_assert (stack_elt >= 2);
835 stack_elt -= 2;
836
837 second = stack[stack_elt];
838 first = stack[stack_elt + 1];
839
840 switch (op)
841 {
842 case DW_OP_and:
843 result = second & first;
844 break;
845 case DW_OP_div:
846 result = (_Unwind_Sword) second / (_Unwind_Sword) first;
847 break;
848 case DW_OP_minus:
849 result = second - first;
850 break;
851 case DW_OP_mod:
852 result = second % first;
853 break;
854 case DW_OP_mul:
855 result = second * first;
856 break;
857 case DW_OP_or:
858 result = second | first;
859 break;
860 case DW_OP_plus:
861 result = second + first;
862 break;
863 case DW_OP_shl:
864 result = second << first;
865 break;
866 case DW_OP_shr:
867 result = second >> first;
868 break;
869 case DW_OP_shra:
870 result = (_Unwind_Sword) second >> first;
871 break;
872 case DW_OP_xor:
873 result = second ^ first;
874 break;
875 case DW_OP_le:
876 result = (_Unwind_Sword) second <= (_Unwind_Sword) first;
877 break;
878 case DW_OP_ge:
879 result = (_Unwind_Sword) second >= (_Unwind_Sword) first;
880 break;
881 case DW_OP_eq:
882 result = (_Unwind_Sword) second == (_Unwind_Sword) first;
883 break;
884 case DW_OP_lt:
885 result = (_Unwind_Sword) second < (_Unwind_Sword) first;
886 break;
887 case DW_OP_gt:
888 result = (_Unwind_Sword) second > (_Unwind_Sword) first;
889 break;
890 case DW_OP_ne:
891 result = (_Unwind_Sword) second != (_Unwind_Sword) first;
892 break;
893
894 default:
895 gcc_unreachable ();
896 }
897 }
898 break;
899
900 case DW_OP_skip:
901 offset = read_2s (op_ptr);
902 op_ptr += 2;
903 op_ptr += offset;
904 goto no_push;
905
906 case DW_OP_bra:
907 gcc_assert (stack_elt);
908 stack_elt -= 1;
909
910 offset = read_2s (op_ptr);
911 op_ptr += 2;
912 if (stack[stack_elt] != 0)
913 op_ptr += offset;
914 goto no_push;
915
916 case DW_OP_nop:
917 goto no_push;
918
919 default:
920 gcc_unreachable ();
921 }
922
923 /* Most things push a result value. */
924 gcc_assert ((size_t) stack_elt < sizeof(stack)/sizeof(*stack));
925 stack[stack_elt++] = result;
926 no_push:;
927 }
928
929 /* We were executing this program to get a value. It should be
930 at top of stack. */
931 gcc_assert (stack_elt);
932 stack_elt -= 1;
933 return stack[stack_elt];
934 }
935
936
937 /* Decode DWARF 2 call frame information. Takes pointers the
938 instruction sequence to decode, current register information and
939 CIE info, and the PC range to evaluate. */
940
941 static void
942 execute_cfa_program (const unsigned char *insn_ptr,
943 const unsigned char *insn_end,
944 struct _Unwind_Context *context,
945 _Unwind_FrameState *fs)
946 {
947 struct frame_state_reg_info *unused_rs = NULL;
948
949 /* Don't allow remember/restore between CIE and FDE programs. */
950 fs->regs.prev = NULL;
951
952 /* The comparison with the return address uses < rather than <= because
953 we are only interested in the effects of code before the call; for a
954 noreturn function, the return address may point to unrelated code with
955 a different stack configuration that we are not interested in. We
956 assume that the call itself is unwind info-neutral; if not, or if
957 there are delay instructions that adjust the stack, these must be
958 reflected at the point immediately before the call insn.
959 In signal frames, return address is after last completed instruction,
960 so we add 1 to return address to make the comparison <=. */
961 while (insn_ptr < insn_end
962 && fs->pc < context->ra + _Unwind_IsSignalFrame (context))
963 {
964 unsigned char insn = *insn_ptr++;
965 _uleb128_t reg, utmp;
966 _sleb128_t offset, stmp;
967
968 if ((insn & 0xc0) == DW_CFA_advance_loc)
969 fs->pc += (insn & 0x3f) * fs->code_align;
970 else if ((insn & 0xc0) == DW_CFA_offset)
971 {
972 reg = insn & 0x3f;
973 insn_ptr = read_uleb128 (insn_ptr, &utmp);
974 offset = (_Unwind_Sword) utmp * fs->data_align;
975 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
976 if (UNWIND_COLUMN_IN_RANGE (reg))
977 {
978 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
979 fs->regs.reg[reg].loc.offset = offset;
980 }
981 }
982 else if ((insn & 0xc0) == DW_CFA_restore)
983 {
984 reg = insn & 0x3f;
985 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
986 if (UNWIND_COLUMN_IN_RANGE (reg))
987 fs->regs.reg[reg].how = REG_UNSAVED;
988 }
989 else switch (insn)
990 {
991 case DW_CFA_set_loc:
992 {
993 _Unwind_Ptr pc;
994
995 insn_ptr = read_encoded_value (context, fs->fde_encoding,
996 insn_ptr, &pc);
997 fs->pc = (void *) pc;
998 }
999 break;
1000
1001 case DW_CFA_advance_loc1:
1002 fs->pc += read_1u (insn_ptr) * fs->code_align;
1003 insn_ptr += 1;
1004 break;
1005 case DW_CFA_advance_loc2:
1006 fs->pc += read_2u (insn_ptr) * fs->code_align;
1007 insn_ptr += 2;
1008 break;
1009 case DW_CFA_advance_loc4:
1010 fs->pc += read_4u (insn_ptr) * fs->code_align;
1011 insn_ptr += 4;
1012 break;
1013
1014 case DW_CFA_offset_extended:
1015 insn_ptr = read_uleb128 (insn_ptr, ®);
1016 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1017 offset = (_Unwind_Sword) utmp * fs->data_align;
1018 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1019 if (UNWIND_COLUMN_IN_RANGE (reg))
1020 {
1021 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1022 fs->regs.reg[reg].loc.offset = offset;
1023 }
1024 break;
1025
1026 case DW_CFA_restore_extended:
1027 insn_ptr = read_uleb128 (insn_ptr, ®);
1028 /* FIXME, this is wrong; the CIE might have said that the
1029 register was saved somewhere. */
1030 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1031 if (UNWIND_COLUMN_IN_RANGE (reg))
1032 fs->regs.reg[reg].how = REG_UNSAVED;
1033 break;
1034
1035 case DW_CFA_same_value:
1036 insn_ptr = read_uleb128 (insn_ptr, ®);
1037 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1038 if (UNWIND_COLUMN_IN_RANGE (reg))
1039 fs->regs.reg[reg].how = REG_UNSAVED;
1040 break;
1041
1042 case DW_CFA_undefined:
1043 insn_ptr = read_uleb128 (insn_ptr, ®);
1044 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1045 if (UNWIND_COLUMN_IN_RANGE (reg))
1046 fs->regs.reg[reg].how = REG_UNDEFINED;
1047 break;
1048
1049 case DW_CFA_nop:
1050 break;
1051
1052 case DW_CFA_register:
1053 {
1054 _uleb128_t reg2;
1055 insn_ptr = read_uleb128 (insn_ptr, ®);
1056 insn_ptr = read_uleb128 (insn_ptr, ®2);
1057 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1058 if (UNWIND_COLUMN_IN_RANGE (reg))
1059 {
1060 fs->regs.reg[reg].how = REG_SAVED_REG;
1061 fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2;
1062 }
1063 }
1064 break;
1065
1066 case DW_CFA_remember_state:
1067 {
1068 struct frame_state_reg_info *new_rs;
1069 if (unused_rs)
1070 {
1071 new_rs = unused_rs;
1072 unused_rs = unused_rs->prev;
1073 }
1074 else
1075 new_rs = alloca (sizeof (struct frame_state_reg_info));
1076
1077 *new_rs = fs->regs;
1078 fs->regs.prev = new_rs;
1079 }
1080 break;
1081
1082 case DW_CFA_restore_state:
1083 {
1084 struct frame_state_reg_info *old_rs = fs->regs.prev;
1085 fs->regs = *old_rs;
1086 old_rs->prev = unused_rs;
1087 unused_rs = old_rs;
1088 }
1089 break;
1090
1091 case DW_CFA_def_cfa:
1092 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1093 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1094 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1095 fs->regs.cfa_offset = (_Unwind_Word)utmp;
1096 fs->regs.cfa_how = CFA_REG_OFFSET;
1097 break;
1098
1099 case DW_CFA_def_cfa_register:
1100 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1101 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1102 fs->regs.cfa_how = CFA_REG_OFFSET;
1103 break;
1104
1105 case DW_CFA_def_cfa_offset:
1106 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1107 fs->regs.cfa_offset = utmp;
1108 /* cfa_how deliberately not set. */
1109 break;
1110
1111 case DW_CFA_def_cfa_expression:
1112 fs->regs.cfa_exp = insn_ptr;
1113 fs->regs.cfa_how = CFA_EXP;
1114 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1115 insn_ptr += utmp;
1116 break;
1117
1118 case DW_CFA_expression:
1119 insn_ptr = read_uleb128 (insn_ptr, ®);
1120 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1121 if (UNWIND_COLUMN_IN_RANGE (reg))
1122 {
1123 fs->regs.reg[reg].how = REG_SAVED_EXP;
1124 fs->regs.reg[reg].loc.exp = insn_ptr;
1125 }
1126 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1127 insn_ptr += utmp;
1128 break;
1129
1130 /* Dwarf3. */
1131 case DW_CFA_offset_extended_sf:
1132 insn_ptr = read_uleb128 (insn_ptr, ®);
1133 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1134 offset = stmp * fs->data_align;
1135 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1136 if (UNWIND_COLUMN_IN_RANGE (reg))
1137 {
1138 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1139 fs->regs.reg[reg].loc.offset = offset;
1140 }
1141 break;
1142
1143 case DW_CFA_def_cfa_sf:
1144 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1145 fs->regs.cfa_reg = (_Unwind_Word)utmp;
1146 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1147 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1148 fs->regs.cfa_how = CFA_REG_OFFSET;
1149 fs->regs.cfa_offset *= fs->data_align;
1150 break;
1151
1152 case DW_CFA_def_cfa_offset_sf:
1153 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1154 fs->regs.cfa_offset = (_Unwind_Sword)stmp;
1155 fs->regs.cfa_offset *= fs->data_align;
1156 /* cfa_how deliberately not set. */
1157 break;
1158
1159 case DW_CFA_val_offset:
1160 insn_ptr = read_uleb128 (insn_ptr, ®);
1161 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1162 offset = (_Unwind_Sword) utmp * fs->data_align;
1163 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1164 if (UNWIND_COLUMN_IN_RANGE (reg))
1165 {
1166 fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
1167 fs->regs.reg[reg].loc.offset = offset;
1168 }
1169 break;
1170
1171 case DW_CFA_val_offset_sf:
1172 insn_ptr = read_uleb128 (insn_ptr, ®);
1173 insn_ptr = read_sleb128 (insn_ptr, &stmp);
1174 offset = stmp * fs->data_align;
1175 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1176 if (UNWIND_COLUMN_IN_RANGE (reg))
1177 {
1178 fs->regs.reg[reg].how = REG_SAVED_VAL_OFFSET;
1179 fs->regs.reg[reg].loc.offset = offset;
1180 }
1181 break;
1182
1183 case DW_CFA_val_expression:
1184 insn_ptr = read_uleb128 (insn_ptr, ®);
1185 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1186 if (UNWIND_COLUMN_IN_RANGE (reg))
1187 {
1188 fs->regs.reg[reg].how = REG_SAVED_VAL_EXP;
1189 fs->regs.reg[reg].loc.exp = insn_ptr;
1190 }
1191 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1192 insn_ptr += utmp;
1193 break;
1194
1195 case DW_CFA_GNU_window_save:
1196 /* ??? Hardcoded for SPARC register window configuration. */
1197 if (DWARF_FRAME_REGISTERS >= 32)
1198 for (reg = 16; reg < 32; ++reg)
1199 {
1200 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1201 fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *);
1202 }
1203 break;
1204
1205 case DW_CFA_GNU_args_size:
1206 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1207 context->args_size = (_Unwind_Word)utmp;
1208 break;
1209
1210 case DW_CFA_GNU_negative_offset_extended:
1211 /* Obsoleted by DW_CFA_offset_extended_sf, but used by
1212 older PowerPC code. */
1213 insn_ptr = read_uleb128 (insn_ptr, ®);
1214 insn_ptr = read_uleb128 (insn_ptr, &utmp);
1215 offset = (_Unwind_Word) utmp * fs->data_align;
1216 reg = DWARF_REG_TO_UNWIND_COLUMN (reg);
1217 if (UNWIND_COLUMN_IN_RANGE (reg))
1218 {
1219 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
1220 fs->regs.reg[reg].loc.offset = -offset;
1221 }
1222 break;
1223
1224 default:
1225 gcc_unreachable ();
1226 }
1227 }
1228 }
1229
1230 /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for
1232 its caller and decode it into FS. This function also sets the
1233 args_size and lsda members of CONTEXT, as they are really information
1234 about the caller's frame. */
1235
1236 static _Unwind_Reason_Code
1237 uw_frame_state_for (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1238 {
1239 const struct dwarf_fde *fde;
1240 const struct dwarf_cie *cie;
1241 const unsigned char *aug, *insn, *end;
1242
1243 memset (fs, 0, sizeof (*fs));
1244 context->args_size = 0;
1245 context->lsda = 0;
1246
1247 if (context->ra == 0)
1248 return _URC_END_OF_STACK;
1249
1250 fde = _Unwind_Find_FDE (context->ra + _Unwind_IsSignalFrame (context) - 1,
1251 &context->bases);
1252 if (fde == NULL)
1253 {
1254 #ifdef MD_FALLBACK_FRAME_STATE_FOR
1255 /* Couldn't find frame unwind info for this function. Try a
1256 target-specific fallback mechanism. This will necessarily
1257 not provide a personality routine or LSDA. */
1258 return MD_FALLBACK_FRAME_STATE_FOR (context, fs);
1259 #else
1260 return _URC_END_OF_STACK;
1261 #endif
1262 }
1263
1264 fs->pc = context->bases.func;
1265
1266 cie = get_cie (fde);
1267 insn = extract_cie_info (cie, context, fs);
1268 if (insn == NULL)
1269 /* CIE contained unknown augmentation. */
1270 return _URC_FATAL_PHASE1_ERROR;
1271
1272 /* First decode all the insns in the CIE. */
1273 end = (const unsigned char *) next_fde ((const struct dwarf_fde *) cie);
1274 execute_cfa_program (insn, end, context, fs);
1275
1276 /* Locate augmentation for the fde. */
1277 aug = (const unsigned char *) fde + sizeof (*fde);
1278 aug += 2 * size_of_encoded_value (fs->fde_encoding);
1279 insn = NULL;
1280 if (fs->saw_z)
1281 {
1282 _uleb128_t i;
1283 aug = read_uleb128 (aug, &i);
1284 insn = aug + i;
1285 }
1286 if (fs->lsda_encoding != DW_EH_PE_omit)
1287 {
1288 _Unwind_Ptr lsda;
1289
1290 aug = read_encoded_value (context, fs->lsda_encoding, aug, &lsda);
1291 context->lsda = (void *) lsda;
1292 }
1293
1294 /* Then the insns in the FDE up to our target PC. */
1295 if (insn == NULL)
1296 insn = aug;
1297 end = (const unsigned char *) next_fde (fde);
1298 execute_cfa_program (insn, end, context, fs);
1299
1300 return _URC_NO_REASON;
1301 }
1302
1303 typedef struct frame_state
1305 {
1306 void *cfa;
1307 void *eh_ptr;
1308 long cfa_offset;
1309 long args_size;
1310 long reg_or_offset[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1311 unsigned short cfa_reg;
1312 unsigned short retaddr_column;
1313 char saved[PRE_GCC3_DWARF_FRAME_REGISTERS+1];
1314 } frame_state;
1315
1316 struct frame_state * __frame_state_for (void *, struct frame_state *);
1317
1318 /* Called from pre-G++ 3.0 __throw to find the registers to restore for
1319 a given PC_TARGET. The caller should allocate a local variable of
1320 `struct frame_state' and pass its address to STATE_IN. */
1321
1322 struct frame_state *
1323 __frame_state_for (void *pc_target, struct frame_state *state_in)
1324 {
1325 struct _Unwind_Context context;
1326 _Unwind_FrameState fs;
1327 int reg;
1328
1329 memset (&context, 0, sizeof (struct _Unwind_Context));
1330 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1331 context.flags = EXTENDED_CONTEXT_BIT;
1332 context.ra = pc_target + 1;
1333
1334 if (uw_frame_state_for (&context, &fs) != _URC_NO_REASON)
1335 return 0;
1336
1337 /* We have no way to pass a location expression for the CFA to our
1338 caller. It wouldn't understand it anyway. */
1339 if (fs.regs.cfa_how == CFA_EXP)
1340 return 0;
1341
1342 for (reg = 0; reg < PRE_GCC3_DWARF_FRAME_REGISTERS + 1; reg++)
1343 {
1344 state_in->saved[reg] = fs.regs.reg[reg].how;
1345 switch (state_in->saved[reg])
1346 {
1347 case REG_SAVED_REG:
1348 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.reg;
1349 break;
1350 case REG_SAVED_OFFSET:
1351 state_in->reg_or_offset[reg] = fs.regs.reg[reg].loc.offset;
1352 break;
1353 default:
1354 state_in->reg_or_offset[reg] = 0;
1355 break;
1356 }
1357 }
1358
1359 state_in->cfa_offset = fs.regs.cfa_offset;
1360 state_in->cfa_reg = fs.regs.cfa_reg;
1361 state_in->retaddr_column = fs.retaddr_column;
1362 state_in->args_size = context.args_size;
1363 state_in->eh_ptr = fs.eh_ptr;
1364
1365 return state_in;
1366 }
1367
1368 typedef union { _Unwind_Ptr ptr; _Unwind_Word word; } _Unwind_SpTmp;
1370
1371 static inline void
1372 _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa,
1373 _Unwind_SpTmp *tmp_sp)
1374 {
1375 int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()];
1376
1377 if (size == sizeof(_Unwind_Ptr))
1378 tmp_sp->ptr = (_Unwind_Ptr) cfa;
1379 else
1380 {
1381 gcc_assert (size == sizeof(_Unwind_Word));
1382 tmp_sp->word = (_Unwind_Ptr) cfa;
1383 }
1384 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), tmp_sp);
1385 }
1386
1387 static void
1388 uw_update_context_1 (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1389 {
1390 struct _Unwind_Context orig_context = *context;
1391 void *cfa;
1392 long i;
1393
1394 #ifdef EH_RETURN_STACKADJ_RTX
1395 /* Special handling here: Many machines do not use a frame pointer,
1396 and track the CFA only through offsets from the stack pointer from
1397 one frame to the next. In this case, the stack pointer is never
1398 stored, so it has no saved address in the context. What we do
1399 have is the CFA from the previous stack frame.
1400
1401 In very special situations (such as unwind info for signal return),
1402 there may be location expressions that use the stack pointer as well.
1403
1404 Do this conditionally for one frame. This allows the unwind info
1405 for one frame to save a copy of the stack pointer from the previous
1406 frame, and be able to use much easier CFA mechanisms to do it.
1407 Always zap the saved stack pointer value for the next frame; carrying
1408 the value over from one frame to another doesn't make sense. */
1409
1410 _Unwind_SpTmp tmp_sp;
1411
1412 if (!_Unwind_GetGRPtr (&orig_context, __builtin_dwarf_sp_column ()))
1413 _Unwind_SetSpColumn (&orig_context, context->cfa, &tmp_sp);
1414 _Unwind_SetGRPtr (context, __builtin_dwarf_sp_column (), NULL);
1415 #endif
1416
1417 /* Compute this frame's CFA. */
1418 switch (fs->regs.cfa_how)
1419 {
1420 case CFA_REG_OFFSET:
1421 cfa = _Unwind_GetPtr (&orig_context, fs->regs.cfa_reg);
1422 cfa += fs->regs.cfa_offset;
1423 break;
1424
1425 case CFA_EXP:
1426 {
1427 const unsigned char *exp = fs->regs.cfa_exp;
1428 _uleb128_t len;
1429
1430 exp = read_uleb128 (exp, &len);
1431 cfa = (void *) (_Unwind_Ptr)
1432 execute_stack_op (exp, exp + len, &orig_context, 0);
1433 break;
1434 }
1435
1436 default:
1437 gcc_unreachable ();
1438 }
1439 context->cfa = cfa;
1440
1441 /* Compute the addresses of all registers saved in this frame. */
1442 for (i = 0; i < DWARF_FRAME_REGISTERS + 1; ++i)
1443 switch (fs->regs.reg[i].how)
1444 {
1445 case REG_UNSAVED:
1446 case REG_UNDEFINED:
1447 break;
1448
1449 case REG_SAVED_OFFSET:
1450 _Unwind_SetGRPtr (context, i,
1451 (void *) (cfa + fs->regs.reg[i].loc.offset));
1452 break;
1453
1454 case REG_SAVED_REG:
1455 if (_Unwind_GRByValue (&orig_context, fs->regs.reg[i].loc.reg))
1456 _Unwind_SetGRValue (context, i,
1457 _Unwind_GetGR (&orig_context,
1458 fs->regs.reg[i].loc.reg));
1459 else
1460 _Unwind_SetGRPtr (context, i,
1461 _Unwind_GetGRPtr (&orig_context,
1462 fs->regs.reg[i].loc.reg));
1463 break;
1464
1465 case REG_SAVED_EXP:
1466 {
1467 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1468 _uleb128_t len;
1469 _Unwind_Ptr val;
1470
1471 exp = read_uleb128 (exp, &len);
1472 val = execute_stack_op (exp, exp + len, &orig_context,
1473 (_Unwind_Ptr) cfa);
1474 _Unwind_SetGRPtr (context, i, (void *) val);
1475 }
1476 break;
1477
1478 case REG_SAVED_VAL_OFFSET:
1479 _Unwind_SetGRValue (context, i,
1480 (_Unwind_Internal_Ptr)
1481 (cfa + fs->regs.reg[i].loc.offset));
1482 break;
1483
1484 case REG_SAVED_VAL_EXP:
1485 {
1486 const unsigned char *exp = fs->regs.reg[i].loc.exp;
1487 _uleb128_t len;
1488 _Unwind_Ptr val;
1489
1490 exp = read_uleb128 (exp, &len);
1491 val = execute_stack_op (exp, exp + len, &orig_context,
1492 (_Unwind_Ptr) cfa);
1493 _Unwind_SetGRValue (context, i, val);
1494 }
1495 break;
1496 }
1497
1498 _Unwind_SetSignalFrame (context, fs->signal_frame);
1499
1500 #ifdef MD_FROB_UPDATE_CONTEXT
1501 MD_FROB_UPDATE_CONTEXT (context, fs);
1502 #endif
1503 }
1504
1505 /* CONTEXT describes the unwind state for a frame, and FS describes the FDE
1506 of its caller. Update CONTEXT to refer to the caller as well. Note
1507 that the args_size and lsda members are not updated here, but later in
1508 uw_frame_state_for. */
1509
1510 static void
1511 uw_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1512 {
1513 uw_update_context_1 (context, fs);
1514
1515 /* In general this unwinder doesn't make any distinction between
1516 undefined and same_value rule. Call-saved registers are assumed
1517 to have same_value rule by default and explicit undefined
1518 rule is handled like same_value. The only exception is
1519 DW_CFA_undefined on retaddr_column which is supposed to
1520 mark outermost frame in DWARF 3. */
1521 if (fs->regs.reg[DWARF_REG_TO_UNWIND_COLUMN (fs->retaddr_column)].how
1522 == REG_UNDEFINED)
1523 /* uw_frame_state_for uses context->ra == 0 check to find outermost
1524 stack frame. */
1525 context->ra = 0;
1526 else
1527 /* Compute the return address now, since the return address column
1528 can change from frame to frame. */
1529 context->ra = __builtin_extract_return_addr
1530 (_Unwind_GetPtr (context, fs->retaddr_column));
1531 }
1532
1533 static void
1534 uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs)
1535 {
1536 uw_update_context (context, fs);
1537 }
1538
1539 /* Fill in CONTEXT for top-of-stack. The only valid registers at this
1541 level will be the return address and the CFA. */
1542
1543 #define uw_init_context(CONTEXT) \
1544 do \
1545 { \
1546 /* Do any necessary initialization to access arbitrary stack frames. \
1547 On the SPARC, this means flushing the register windows. */ \
1548 __builtin_unwind_init (); \
1549 uw_init_context_1 (CONTEXT, __builtin_dwarf_cfa (), \
1550 __builtin_return_address (0)); \
1551 } \
1552 while (0)
1553
1554 static inline void
1555 init_dwarf_reg_size_table (void)
1556 {
1557 __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table);
1558 }
1559
1560 static void __attribute__((noinline))
1561 uw_init_context_1 (struct _Unwind_Context *context,
1562 void *outer_cfa, void *outer_ra)
1563 {
1564 void *ra = __builtin_extract_return_addr (__builtin_return_address (0));
1565 _Unwind_FrameState fs;
1566 _Unwind_SpTmp sp_slot;
1567 _Unwind_Reason_Code code;
1568
1569 memset (context, 0, sizeof (struct _Unwind_Context));
1570 context->ra = ra;
1571 if (!ASSUME_EXTENDED_UNWIND_CONTEXT)
1572 context->flags = EXTENDED_CONTEXT_BIT;
1573
1574 code = uw_frame_state_for (context, &fs);
1575 gcc_assert (code == _URC_NO_REASON);
1576
1577 #if __GTHREADS
1578 {
1579 static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT;
1580 if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0
1581 && dwarf_reg_size_table[0] == 0)
1582 init_dwarf_reg_size_table ();
1583 }
1584 #else
1585 if (dwarf_reg_size_table[0] == 0)
1586 init_dwarf_reg_size_table ();
1587 #endif
1588
1589 /* Force the frame state to use the known cfa value. */
1590 _Unwind_SetSpColumn (context, outer_cfa, &sp_slot);
1591 fs.regs.cfa_how = CFA_REG_OFFSET;
1592 fs.regs.cfa_reg = __builtin_dwarf_sp_column ();
1593 fs.regs.cfa_offset = 0;
1594
1595 uw_update_context_1 (context, &fs);
1596
1597 /* If the return address column was saved in a register in the
1598 initialization context, then we can't see it in the given
1599 call frame data. So have the initialization context tell us. */
1600 context->ra = __builtin_extract_return_addr (outer_ra);
1601 }
1602
1603 static void _Unwind_DebugHook (void *, void *)
1604 __attribute__ ((__noinline__, __used__, __noclone__));
1605
1606 /* This function is called during unwinding. It is intended as a hook
1607 for a debugger to intercept exceptions. CFA is the CFA of the
1608 target frame. HANDLER is the PC to which control will be
1609 transferred. */
1610 static void
1611 _Unwind_DebugHook (void *cfa __attribute__ ((__unused__)),
1612 void *handler __attribute__ ((__unused__)))
1613 {
1614 /* We only want to use stap probes starting with v3. Earlier
1615 versions added too much startup cost. */
1616 #if defined (HAVE_SYS_SDT_H) && defined (STAP_PROBE2) && _SDT_NOTE_TYPE >= 3
1617 STAP_PROBE2 (libgcc, unwind, cfa, handler);
1618 #else
1619 asm ("");
1620 #endif
1621 }
1622
1623 /* Install TARGET into CURRENT so that we can return to it. This is a
1624 macro because __builtin_eh_return must be invoked in the context of
1625 our caller. */
1626
1627 #define uw_install_context(CURRENT, TARGET) \
1628 do \
1629 { \
1630 long offset = uw_install_context_1 ((CURRENT), (TARGET)); \
1631 void *handler = __builtin_frob_return_addr ((TARGET)->ra); \
1632 _Unwind_DebugHook ((TARGET)->cfa, handler); \
1633 __builtin_eh_return (offset, handler); \
1634 } \
1635 while (0)
1636
1637 static long
1638 uw_install_context_1 (struct _Unwind_Context *current,
1639 struct _Unwind_Context *target)
1640 {
1641 long i;
1642 _Unwind_SpTmp sp_slot;
1643
1644 /* If the target frame does not have a saved stack pointer,
1645 then set up the target's CFA. */
1646 if (!_Unwind_GetGRPtr (target, __builtin_dwarf_sp_column ()))
1647 _Unwind_SetSpColumn (target, target->cfa, &sp_slot);
1648
1649 for (i = 0; i < DWARF_FRAME_REGISTERS; ++i)
1650 {
1651 void *c = (void *) (_Unwind_Internal_Ptr) current->reg[i];
1652 void *t = (void *) (_Unwind_Internal_Ptr)target->reg[i];
1653
1654 gcc_assert (current->by_value[i] == 0);
1655 if (target->by_value[i] && c)
1656 {
1657 _Unwind_Word w;
1658 _Unwind_Ptr p;
1659 if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word))
1660 {
1661 w = (_Unwind_Internal_Ptr) t;
1662 memcpy (c, &w, sizeof (_Unwind_Word));
1663 }
1664 else
1665 {
1666 gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr));
1667 p = (_Unwind_Internal_Ptr) t;
1668 memcpy (c, &p, sizeof (_Unwind_Ptr));
1669 }
1670 }
1671 else if (t && c && t != c)
1672 memcpy (c, t, dwarf_reg_size_table[i]);
1673 }
1674
1675 /* If the current frame doesn't have a saved stack pointer, then we
1676 need to rely on EH_RETURN_STACKADJ_RTX to get our target stack
1677 pointer value reloaded. */
1678 if (!_Unwind_GetGRPtr (current, __builtin_dwarf_sp_column ()))
1679 {
1680 void *target_cfa;
1681
1682 target_cfa = _Unwind_GetPtr (target, __builtin_dwarf_sp_column ());
1683
1684 /* We adjust SP by the difference between CURRENT and TARGET's CFA. */
1685 if (STACK_GROWS_DOWNWARD)
1686 return target_cfa - current->cfa + target->args_size;
1687 else
1688 return current->cfa - target_cfa - target->args_size;
1689 }
1690 return 0;
1691 }
1692
1693 static inline _Unwind_Ptr
1694 uw_identify_context (struct _Unwind_Context *context)
1695 {
1696 /* The CFA is not sufficient to disambiguate the context of a function
1697 interrupted by a signal before establishing its frame and the context
1698 of the signal itself. */
1699 if (STACK_GROWS_DOWNWARD)
1700 return _Unwind_GetCFA (context) - _Unwind_IsSignalFrame (context);
1701 else
1702 return _Unwind_GetCFA (context) + _Unwind_IsSignalFrame (context);
1703 }
1704
1705
1706 #include "unwind.inc"
1707
1708 #if defined (USE_GAS_SYMVER) && defined (SHARED) && defined (USE_LIBUNWIND_EXCEPTIONS)
1709 alias (_Unwind_Backtrace);
1710 alias (_Unwind_DeleteException);
1711 alias (_Unwind_FindEnclosingFunction);
1712 alias (_Unwind_ForcedUnwind);
1713 alias (_Unwind_GetDataRelBase);
1714 alias (_Unwind_GetTextRelBase);
1715 alias (_Unwind_GetCFA);
1716 alias (_Unwind_GetGR);
1717 alias (_Unwind_GetIP);
1718 alias (_Unwind_GetLanguageSpecificData);
1719 alias (_Unwind_GetRegionStart);
1720 alias (_Unwind_RaiseException);
1721 alias (_Unwind_Resume);
1722 alias (_Unwind_Resume_or_Rethrow);
1723 alias (_Unwind_SetGR);
1724 alias (_Unwind_SetIP);
1725 #endif
1726
1727 #endif /* !USING_SJLJ_EXCEPTIONS */
1728