1 /* Exception handling and frame unwind runtime interface routines.
2 Copyright (C) 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* @@@ Really this should be out of line, but this also causes link
22 compatibility problems with the base ABI. This is slightly better
23 than duplicating code, however. */
25 /* Pointer encodings, from dwarf2.h. */
26 #define DW_EH_PE_absptr 0x00
27 #define DW_EH_PE_omit 0xff
29 #define DW_EH_PE_uleb128 0x01
30 #define DW_EH_PE_udata2 0x02
31 #define DW_EH_PE_udata4 0x03
32 #define DW_EH_PE_udata8 0x04
33 #define DW_EH_PE_sleb128 0x09
34 #define DW_EH_PE_sdata2 0x0A
35 #define DW_EH_PE_sdata4 0x0B
36 #define DW_EH_PE_sdata8 0x0C
37 #define DW_EH_PE_signed 0x08
39 #define DW_EH_PE_pcrel 0x10
40 #define DW_EH_PE_textrel 0x20
41 #define DW_EH_PE_datarel 0x30
42 #define DW_EH_PE_funcrel 0x40
44 #define DW_EH_PE_indirect 0x80
47 size_of_encoded_value (unsigned char encoding)
49 if (encoding == DW_EH_PE_omit)
52 switch (encoding & 0x07)
55 return sizeof (void *);
67 base_of_encoded_value (unsigned char encoding, _Unwind_Context *context)
69 if (encoding == DW_EH_PE_omit)
72 switch (encoding & 0x70)
78 case DW_EH_PE_textrel:
79 return _Unwind_GetTextRelBase (context);
80 case DW_EH_PE_datarel:
81 return _Unwind_GetDataRelBase (context);
82 case DW_EH_PE_funcrel:
83 return _Unwind_GetRegionStart (context);
88 static const unsigned char *
89 read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
90 const unsigned char *p, _Unwind_Ptr *val)
95 unsigned u2 __attribute__ ((mode (HI)));
96 unsigned u4 __attribute__ ((mode (SI)));
97 unsigned u8 __attribute__ ((mode (DI)));
98 signed s2 __attribute__ ((mode (HI)));
99 signed s4 __attribute__ ((mode (SI)));
100 signed s8 __attribute__ ((mode (DI)));
101 } __attribute__((__packed__));
103 union unaligned *u = (union unaligned *) p;
106 switch (encoding & 0x0f)
108 case DW_EH_PE_absptr:
109 result = (_Unwind_Ptr) u->ptr;
110 p += sizeof (void *);
113 case DW_EH_PE_uleb128:
115 unsigned int shift = 0;
122 result |= (_Unwind_Ptr)(byte & 0x7f) << shift;
129 case DW_EH_PE_sleb128:
131 unsigned int shift = 0;
138 result |= (_Unwind_Ptr)(byte & 0x7f) << shift;
143 if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
144 result |= -(1L << shift);
148 case DW_EH_PE_udata2:
152 case DW_EH_PE_udata4:
156 case DW_EH_PE_udata8:
161 case DW_EH_PE_sdata2:
165 case DW_EH_PE_sdata4:
169 case DW_EH_PE_sdata8:
180 result += ((encoding & 0x70) == DW_EH_PE_pcrel ? (_Unwind_Ptr)u : base);
181 if (encoding & DW_EH_PE_indirect)
182 result = *(_Unwind_Ptr *)result;
189 static inline const unsigned char *
190 read_encoded_value (_Unwind_Context *context, unsigned char encoding,
191 const unsigned char *p, _Unwind_Ptr *val)
193 return read_encoded_value_with_base (encoding,
194 base_of_encoded_value (encoding, context),
198 static inline const unsigned char *
199 read_uleb128 (const unsigned char *p, _Unwind_Ptr *val)
201 return read_encoded_value_with_base (DW_EH_PE_uleb128, 0, p, val);
204 static inline const unsigned char *
205 read_sleb128 (const unsigned char *p, _Unwind_Ptr *val)
207 return read_encoded_value_with_base (DW_EH_PE_sleb128, 0, p, val);