OSDN Git Service

* real.h (ieee_extended_intel_96_round_53_format): New.
[pf3gnuchains/gcc-fork.git] / gcc / unwind-pe.h
1 /* Exception handling and frame unwind runtime interface routines.
2    Copyright (C) 2001, 2002 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 2, 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    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING.  If not, write to the Free
18    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.  */
20
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.  */
24
25 /* If using C++, references to abort have to be qualified with std::.  */
26 #if __cplusplus
27 #define __gxx_abort std::abort
28 #else
29 #define __gxx_abort abort
30 #endif
31
32 /* Pointer encodings, from dwarf2.h.  */
33 #define DW_EH_PE_absptr         0x00
34 #define DW_EH_PE_omit           0xff
35
36 #define DW_EH_PE_uleb128        0x01
37 #define DW_EH_PE_udata2         0x02
38 #define DW_EH_PE_udata4         0x03
39 #define DW_EH_PE_udata8         0x04
40 #define DW_EH_PE_sleb128        0x09
41 #define DW_EH_PE_sdata2         0x0A
42 #define DW_EH_PE_sdata4         0x0B
43 #define DW_EH_PE_sdata8         0x0C
44 #define DW_EH_PE_signed         0x08
45
46 #define DW_EH_PE_pcrel          0x10
47 #define DW_EH_PE_textrel        0x20
48 #define DW_EH_PE_datarel        0x30
49 #define DW_EH_PE_funcrel        0x40
50 #define DW_EH_PE_aligned        0x50
51
52 #define DW_EH_PE_indirect       0x80
53 \f
54
55 #ifndef NO_SIZE_OF_ENCODED_VALUE
56
57 /* Given an encoding, return the number of bytes the format occupies.
58    This is only defined for fixed-size encodings, and so does not
59    include leb128.  */
60
61 static unsigned int
62 size_of_encoded_value (unsigned char encoding)
63 {
64   if (encoding == DW_EH_PE_omit)
65     return 0;
66
67   switch (encoding & 0x07)
68     {
69     case DW_EH_PE_absptr:
70       return sizeof (void *);
71     case DW_EH_PE_udata2:
72       return 2;
73     case DW_EH_PE_udata4:
74       return 4;
75     case DW_EH_PE_udata8:
76       return 8;
77     }
78   __gxx_abort ();
79 }
80
81 #endif
82
83 #ifndef NO_BASE_OF_ENCODED_VALUE
84
85 /* Given an encoding and an _Unwind_Context, return the base to which
86    the encoding is relative.  This base may then be passed to
87    read_encoded_value_with_base for use when the _Unwind_Context is
88    not available.  */
89
90 static _Unwind_Ptr
91 base_of_encoded_value (unsigned char encoding, struct _Unwind_Context *context)
92 {
93   if (encoding == DW_EH_PE_omit)
94     return 0;
95
96   switch (encoding & 0x70)
97     {
98     case DW_EH_PE_absptr:
99     case DW_EH_PE_pcrel:
100     case DW_EH_PE_aligned:
101       return 0;
102
103     case DW_EH_PE_textrel:
104       return _Unwind_GetTextRelBase (context);
105     case DW_EH_PE_datarel:
106       return _Unwind_GetDataRelBase (context);
107     case DW_EH_PE_funcrel:
108       return _Unwind_GetRegionStart (context);
109     }
110   __gxx_abort ();
111 }
112
113 #endif
114
115 /* Read an unsigned leb128 value from P, store the value in VAL, return
116    P incremented past the value.  We assume that a word is large enough to
117    hold any value so encoded; if it is smaller than a pointer on some target,
118    pointers should not be leb128 encoded on that target.  */
119
120 static const unsigned char *
121 read_uleb128 (const unsigned char *p, _Unwind_Word *val)
122 {
123   unsigned int shift = 0;
124   unsigned char byte;
125   _Unwind_Word result;
126
127   result = 0;
128   do
129     {
130       byte = *p++;
131       result |= (byte & 0x7f) << shift;
132       shift += 7;
133     }
134   while (byte & 0x80);
135
136   *val = result;
137   return p;
138 }
139
140 /* Similar, but read a signed leb128 value.  */
141
142 static const unsigned char *
143 read_sleb128 (const unsigned char *p, _Unwind_Sword *val)
144 {
145   unsigned int shift = 0;
146   unsigned char byte;
147   _Unwind_Word result;
148
149   result = 0;
150   do
151     {
152       byte = *p++;
153       result |= (byte & 0x7f) << shift;
154       shift += 7;
155     }
156   while (byte & 0x80);
157
158   /* Sign-extend a negative value.  */
159   if (shift < 8 * sizeof(result) && (byte & 0x40) != 0)
160     result |= -(1L << shift);
161
162   *val = (_Unwind_Sword) result;
163   return p;
164 }
165
166 /* Load an encoded value from memory at P.  The value is returned in VAL;
167    The function returns P incremented past the value.  BASE is as given
168    by base_of_encoded_value for this encoding in the appropriate context.  */
169
170 static const unsigned char *
171 read_encoded_value_with_base (unsigned char encoding, _Unwind_Ptr base,
172                               const unsigned char *p, _Unwind_Ptr *val)
173 {
174   union unaligned
175     {
176       void *ptr;
177       unsigned u2 __attribute__ ((mode (HI)));
178       unsigned u4 __attribute__ ((mode (SI)));
179       unsigned u8 __attribute__ ((mode (DI)));
180       signed s2 __attribute__ ((mode (HI)));
181       signed s4 __attribute__ ((mode (SI)));
182       signed s8 __attribute__ ((mode (DI)));
183     } __attribute__((__packed__));
184
185   union unaligned *u = (union unaligned *) p;
186   _Unwind_Internal_Ptr result;
187
188   if (encoding == DW_EH_PE_aligned)
189     {
190       _Unwind_Internal_Ptr a = (_Unwind_Internal_Ptr) p;
191       a = (a + sizeof (void *) - 1) & - sizeof(void *);
192       result = *(_Unwind_Internal_Ptr *) a;
193       p = (const unsigned char *) (a + sizeof (void *));
194     }
195   else
196     {
197       switch (encoding & 0x0f)
198         {
199         case DW_EH_PE_absptr:
200           result = (_Unwind_Internal_Ptr) u->ptr;
201           p += sizeof (void *);
202           break;
203
204         case DW_EH_PE_uleb128:
205           {
206             _Unwind_Word tmp;
207             p = read_uleb128 (p, &tmp);
208             result = (_Unwind_Internal_Ptr) tmp;
209           }
210           break;
211
212         case DW_EH_PE_sleb128:
213           {
214             _Unwind_Sword tmp;
215             p = read_sleb128 (p, &tmp);
216             result = (_Unwind_Internal_Ptr) tmp;
217           }
218           break;
219
220         case DW_EH_PE_udata2:
221           result = u->u2;
222           p += 2;
223           break;
224         case DW_EH_PE_udata4:
225           result = u->u4;
226           p += 4;
227           break;
228         case DW_EH_PE_udata8:
229           result = u->u8;
230           p += 8;
231           break;
232
233         case DW_EH_PE_sdata2:
234           result = u->s2;
235           p += 2;
236           break;
237         case DW_EH_PE_sdata4:
238           result = u->s4;
239           p += 4;
240           break;
241         case DW_EH_PE_sdata8:
242           result = u->s8;
243           p += 8;
244           break;
245
246         default:
247           __gxx_abort ();
248         }
249
250       if (result != 0)
251         {
252           result += ((encoding & 0x70) == DW_EH_PE_pcrel
253                      ? (_Unwind_Internal_Ptr) u : base);
254           if (encoding & DW_EH_PE_indirect)
255             result = *(_Unwind_Internal_Ptr *) result;
256         }
257     }
258
259   *val = result;
260   return p;
261 }
262
263 #ifndef NO_BASE_OF_ENCODED_VALUE
264
265 /* Like read_encoded_value_with_base, but get the base from the context
266    rather than providing it directly.  */
267
268 static inline const unsigned char *
269 read_encoded_value (struct _Unwind_Context *context, unsigned char encoding,
270                     const unsigned char *p, _Unwind_Ptr *val)
271 {
272   return read_encoded_value_with_base (encoding,
273                 base_of_encoded_value (encoding, context),
274                 p, val);
275 }
276
277 #endif