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