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