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