Subversion Repositories Kolibri OS

Rev

Rev 6515 | Details | Compare with Previous | Last modification | View Log | RSS feed

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