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