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