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