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