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