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 /* Given an encoding, return the number of bytes the format occupies.
48 This is only defined for fixed-size encodings, and so does not
52 size_of_encoded_value (unsigned char encoding
)
54 if (encoding
== DW_EH_PE_omit
)
57 switch (encoding
& 0x07)
60 return sizeof (void *);
71 /* Given an encoding and an _Unwind_Context, return the base to which
72 the encoding is relative. This base may then be passed to
73 read_encoded_value_with_base for use when the _Unwind_Context is
77 base_of_encoded_value (unsigned char encoding
, _Unwind_Context
*context
)
79 if (encoding
== DW_EH_PE_omit
)
82 switch (encoding
& 0x70)
88 case DW_EH_PE_textrel
:
89 return _Unwind_GetTextRelBase (context
);
90 case DW_EH_PE_datarel
:
91 return _Unwind_GetDataRelBase (context
);
92 case DW_EH_PE_funcrel
:
93 return _Unwind_GetRegionStart (context
);
98 /* Load an encoded value from memory at P. The value is returned in VAL;
99 The function returns P incremented past the value. BASE is as given
100 by base_of_encoded_value for this encoding in the appropriate context. */
102 static const unsigned char *
103 read_encoded_value_with_base (unsigned char encoding
, _Unwind_Ptr base
,
104 const unsigned char *p
, _Unwind_Ptr
*val
)
109 unsigned u2
__attribute__ ((mode (HI
)));
110 unsigned u4
__attribute__ ((mode (SI
)));
111 unsigned u8
__attribute__ ((mode (DI
)));
112 signed s2
__attribute__ ((mode (HI
)));
113 signed s4
__attribute__ ((mode (SI
)));
114 signed s8
__attribute__ ((mode (DI
)));
115 } __attribute__((__packed__
));
117 union unaligned
*u
= (union unaligned
*) p
;
120 switch (encoding
& 0x0f)
122 case DW_EH_PE_absptr
:
123 result
= (_Unwind_Ptr
) u
->ptr
;
124 p
+= sizeof (void *);
127 case DW_EH_PE_uleb128
:
129 unsigned int shift
= 0;
136 result
|= (_Unwind_Ptr
)(byte
& 0x7f) << shift
;
143 case DW_EH_PE_sleb128
:
145 unsigned int shift
= 0;
152 result
|= (_Unwind_Ptr
)(byte
& 0x7f) << shift
;
157 if (shift
< 8 * sizeof(result
) && (byte
& 0x40) != 0)
158 result
|= -(1L << shift
);
162 case DW_EH_PE_udata2
:
166 case DW_EH_PE_udata4
:
170 case DW_EH_PE_udata8
:
175 case DW_EH_PE_sdata2
:
179 case DW_EH_PE_sdata4
:
183 case DW_EH_PE_sdata8
:
194 result
+= ((encoding
& 0x70) == DW_EH_PE_pcrel
? (_Unwind_Ptr
)u
: base
);
195 if (encoding
& DW_EH_PE_indirect
)
196 result
= *(_Unwind_Ptr
*)result
;
203 /* Like read_encoded_value_with_base, but get the base from the context
204 rather than providing it directly. */
206 static inline const unsigned char *
207 read_encoded_value (_Unwind_Context
*context
, unsigned char encoding
,
208 const unsigned char *p
, _Unwind_Ptr
*val
)
210 return read_encoded_value_with_base (encoding
,
211 base_of_encoded_value (encoding
, context
),
215 /* Read an unsigned leb128 value from P, store the value in VAL, return
216 P incremented past the value. */
218 static inline const unsigned char *
219 read_uleb128 (const unsigned char *p
, _Unwind_Ptr
*val
)
221 return read_encoded_value_with_base (DW_EH_PE_uleb128
, 0, p
, val
);
224 /* Similar, but read a signed leb128 value. */
226 static inline const unsigned char *
227 read_sleb128 (const unsigned char *p
, _Unwind_Ptr
*val
)
229 return read_encoded_value_with_base (DW_EH_PE_sleb128
, 0, p
, val
);