]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/unwind-c.c
re PR target/55171 (incorrect virtual thunk on mingw)
[thirdparty/gcc.git] / libgcc / unwind-c.c
CommitLineData
0bfa5f65 1/* Supporting functions for C exception handling.
748086b7 2 Copyright (C) 2002, 2003, 2009 Free Software Foundation, Inc.
0bfa5f65
RH
3 Contributed by Aldy Hernandez <aldy@quesejoda.com>.
4 Shamelessly stolen from the Java front end.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
748086b7 10Software Foundation; either version 3, or (at your option) any later
0bfa5f65
RH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
748086b7
JJ
18Under Section 7 of GPL version 3, you are granted additional
19permissions described in the GCC Runtime Library Exception, version
203.1, as published by the Free Software Foundation.
21
22You should have received a copy of the GNU General Public License and
23a copy of the GCC Runtime Library Exception along with this program;
24see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25<http://www.gnu.org/licenses/>. */
0bfa5f65
RH
26
27#include "tconfig.h"
28#include "tsystem.h"
29#include "unwind.h"
333991cf 30#define NO_SIZE_OF_ENCODED_VALUE
0bfa5f65
RH
31#include "unwind-pe.h"
32
33typedef struct
34{
35 _Unwind_Ptr Start;
36 _Unwind_Ptr LPStart;
37 _Unwind_Ptr ttype_base;
38 const unsigned char *TType;
39 const unsigned char *action_table;
40 unsigned char ttype_encoding;
41 unsigned char call_site_encoding;
42} lsda_header_info;
43
44static const unsigned char *
45parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
46 lsda_header_info *info)
47{
f767122b 48 _uleb128_t tmp;
0bfa5f65
RH
49 unsigned char lpstart_encoding;
50
51 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
52
53 /* Find @LPStart, the base to which landing pad offsets are relative. */
54 lpstart_encoding = *p++;
55 if (lpstart_encoding != DW_EH_PE_omit)
56 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
57 else
58 info->LPStart = info->Start;
59
60 /* Find @TType, the base of the handler and exception spec type data. */
61 info->ttype_encoding = *p++;
62 if (info->ttype_encoding != DW_EH_PE_omit)
63 {
64 p = read_uleb128 (p, &tmp);
65 info->TType = p + tmp;
66 }
67 else
68 info->TType = 0;
69
70 /* The encoding and length of the call-site table; the action table
71 immediately follows. */
72 info->call_site_encoding = *p++;
73 p = read_uleb128 (p, &tmp);
74 info->action_table = p + tmp;
75
76 return p;
77}
78
617a1b71
PB
79#ifdef __ARM_EABI_UNWINDER__
80/* ARM EABI personality routines must also unwind the stack. */
81#define CONTINUE_UNWINDING \
82 do \
83 { \
84 if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
85 return _URC_FAILURE; \
86 return _URC_CONTINUE_UNWIND; \
87 } \
88 while (0)
89#else
90#define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
91#endif
92
0bfa5f65
RH
93#ifdef __USING_SJLJ_EXCEPTIONS__
94#define PERSONALITY_FUNCTION __gcc_personality_sj0
95#define __builtin_eh_return_data_regno(x) x
bf1431e3
TG
96#elif defined(__SEH__)
97#define PERSONALITY_FUNCTION __gcc_personality_imp
0bfa5f65
RH
98#else
99#define PERSONALITY_FUNCTION __gcc_personality_v0
100#endif
0bfa5f65 101
617a1b71
PB
102#ifdef __ARM_EABI_UNWINDER__
103_Unwind_Reason_Code
104PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
105 struct _Unwind_Context *);
106
107_Unwind_Reason_Code
108PERSONALITY_FUNCTION (_Unwind_State state,
109 struct _Unwind_Exception * ue_header,
110 struct _Unwind_Context * context)
111#else
bf1431e3
TG
112#ifdef __SEH__
113static
114#endif
0bfa5f65
RH
115_Unwind_Reason_Code
116PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
117 struct _Unwind_Exception *, struct _Unwind_Context *);
118
119_Unwind_Reason_Code
120PERSONALITY_FUNCTION (int version,
121 _Unwind_Action actions,
122 _Unwind_Exception_Class exception_class ATTRIBUTE_UNUSED,
123 struct _Unwind_Exception *ue_header,
124 struct _Unwind_Context *context)
617a1b71 125#endif
0bfa5f65
RH
126{
127 lsda_header_info info;
f1ed99cd 128 const unsigned char *language_specific_data, *p;
0bfa5f65 129 _Unwind_Ptr landing_pad, ip;
754e45a8 130 int ip_before_insn = 0;
0bfa5f65 131
617a1b71 132#ifdef __ARM_EABI_UNWINDER__
74d9c39f 133 if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
617a1b71
PB
134 CONTINUE_UNWINDING;
135
136 /* The dwarf unwinder assumes the context structure holds things like the
137 function and LSDA pointers. The ARM implementation caches these in
1e874273
PB
138 the exception header (UCB). To avoid rewriting everything we make a
139 virtual scratch register point at the UCB. */
617a1b71 140 ip = (_Unwind_Ptr) ue_header;
1e874273 141 _Unwind_SetGR (context, UNWIND_POINTER_REG, ip);
617a1b71 142#else
0bfa5f65
RH
143 if (version != 1)
144 return _URC_FATAL_PHASE1_ERROR;
145
146 /* Currently we only support cleanups for C. */
147 if ((actions & _UA_CLEANUP_PHASE) == 0)
617a1b71
PB
148 CONTINUE_UNWINDING;
149#endif
0bfa5f65
RH
150
151 language_specific_data = (const unsigned char *)
152 _Unwind_GetLanguageSpecificData (context);
153
154 /* If no LSDA, then there are no handlers or cleanups. */
155 if (! language_specific_data)
617a1b71 156 CONTINUE_UNWINDING;
0bfa5f65
RH
157
158 /* Parse the LSDA header. */
159 p = parse_lsda_header (context, language_specific_data, &info);
e64f5acf 160#ifdef HAVE_GETIPINFO
754e45a8 161 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
e64f5acf
SE
162#else
163 ip = _Unwind_GetIP (context);
164#endif
754e45a8
JJ
165 if (! ip_before_insn)
166 --ip;
0bfa5f65
RH
167 landing_pad = 0;
168
169#ifdef __USING_SJLJ_EXCEPTIONS__
170 /* The given "IP" is an index into the call-site table, with two
171 exceptions -- -1 means no-action, and 0 means terminate. But
172 since we're using uleb128 values, we've not got random access
173 to the array. */
174 if ((int) ip <= 0)
175 return _URC_CONTINUE_UNWIND;
176 else
177 {
f767122b 178 _uleb128_t cs_lp, cs_action;
0bfa5f65
RH
179 do
180 {
181 p = read_uleb128 (p, &cs_lp);
182 p = read_uleb128 (p, &cs_action);
183 }
184 while (--ip);
185
186 /* Can never have null landing pad for sjlj -- that would have
187 been indicated by a -1 call site index. */
f767122b 188 landing_pad = (_Unwind_Ptr)cs_lp + 1;
0bfa5f65
RH
189 goto found_something;
190 }
191#else
192 /* Search the call-site table for the action associated with this IP. */
193 while (p < info.action_table)
194 {
195 _Unwind_Ptr cs_start, cs_len, cs_lp;
f767122b 196 _uleb128_t cs_action;
0bfa5f65
RH
197
198 /* Note that all call-site encodings are "absolute" displacements. */
199 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
200 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
201 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
202 p = read_uleb128 (p, &cs_action);
203
204 /* The table is sorted, so if we've passed the ip, stop. */
205 if (ip < info.Start + cs_start)
206 p = info.action_table;
207 else if (ip < info.Start + cs_start + cs_len)
208 {
209 if (cs_lp)
210 landing_pad = info.LPStart + cs_lp;
0bfa5f65
RH
211 goto found_something;
212 }
213 }
0bfa5f65
RH
214#endif
215
216 /* IP is not in table. No associated cleanups. */
217 /* ??? This is where C++ calls std::terminate to catch throw
218 from a destructor. */
617a1b71 219 CONTINUE_UNWINDING;
0bfa5f65
RH
220
221 found_something:
222 if (landing_pad == 0)
223 {
224 /* IP is present, but has a null landing pad.
225 No handler to be run. */
617a1b71 226 CONTINUE_UNWINDING;
0bfa5f65
RH
227 }
228
229 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
230 (_Unwind_Ptr) ue_header);
231 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
232 _Unwind_SetIP (context, landing_pad);
233 return _URC_INSTALL_CONTEXT;
234}
bf1431e3
TG
235
236#ifdef __SEH__
237EXCEPTION_DISPOSITION
238__gcc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
239 PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
240{
241 return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
242 ms_disp, __gcc_personality_imp);
243}
244#endif /* SEH */