]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/unwind-c.c
Update copyright years.
[thirdparty/gcc.git] / libgcc / unwind-c.c
CommitLineData
0bfa5f65 1/* Supporting functions for C exception handling.
85ec4feb 2 Copyright (C) 2002-2018 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"
33107571 29#include "auto-target.h"
0bfa5f65 30#include "unwind.h"
333991cf 31#define NO_SIZE_OF_ENCODED_VALUE
0bfa5f65
RH
32#include "unwind-pe.h"
33
34typedef struct
35{
36 _Unwind_Ptr Start;
37 _Unwind_Ptr LPStart;
38 _Unwind_Ptr ttype_base;
39 const unsigned char *TType;
40 const unsigned char *action_table;
41 unsigned char ttype_encoding;
42 unsigned char call_site_encoding;
43} lsda_header_info;
44
45static const unsigned char *
46parse_lsda_header (struct _Unwind_Context *context, const unsigned char *p,
47 lsda_header_info *info)
48{
f767122b 49 _uleb128_t tmp;
0bfa5f65
RH
50 unsigned char lpstart_encoding;
51
52 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
53
54 /* Find @LPStart, the base to which landing pad offsets are relative. */
55 lpstart_encoding = *p++;
56 if (lpstart_encoding != DW_EH_PE_omit)
57 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
58 else
59 info->LPStart = info->Start;
60
61 /* Find @TType, the base of the handler and exception spec type data. */
62 info->ttype_encoding = *p++;
63 if (info->ttype_encoding != DW_EH_PE_omit)
64 {
65 p = read_uleb128 (p, &tmp);
66 info->TType = p + tmp;
67 }
68 else
69 info->TType = 0;
70
71 /* The encoding and length of the call-site table; the action table
72 immediately follows. */
73 info->call_site_encoding = *p++;
74 p = read_uleb128 (p, &tmp);
75 info->action_table = p + tmp;
76
77 return p;
78}
79
617a1b71
PB
80#ifdef __ARM_EABI_UNWINDER__
81/* ARM EABI personality routines must also unwind the stack. */
82#define CONTINUE_UNWINDING \
83 do \
84 { \
85 if (__gnu_unwind_frame (ue_header, context) != _URC_OK) \
86 return _URC_FAILURE; \
87 return _URC_CONTINUE_UNWIND; \
88 } \
89 while (0)
90#else
91#define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
92#endif
93
0bfa5f65
RH
94#ifdef __USING_SJLJ_EXCEPTIONS__
95#define PERSONALITY_FUNCTION __gcc_personality_sj0
96#define __builtin_eh_return_data_regno(x) x
bf1431e3
TG
97#elif defined(__SEH__)
98#define PERSONALITY_FUNCTION __gcc_personality_imp
0bfa5f65
RH
99#else
100#define PERSONALITY_FUNCTION __gcc_personality_v0
101#endif
0bfa5f65 102
617a1b71
PB
103#ifdef __ARM_EABI_UNWINDER__
104_Unwind_Reason_Code
105PERSONALITY_FUNCTION (_Unwind_State, struct _Unwind_Exception *,
106 struct _Unwind_Context *);
107
108_Unwind_Reason_Code
109PERSONALITY_FUNCTION (_Unwind_State state,
110 struct _Unwind_Exception * ue_header,
111 struct _Unwind_Context * context)
112#else
e5a81c8e 113#if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
bf1431e3
TG
114static
115#endif
0bfa5f65
RH
116_Unwind_Reason_Code
117PERSONALITY_FUNCTION (int, _Unwind_Action, _Unwind_Exception_Class,
118 struct _Unwind_Exception *, struct _Unwind_Context *);
119
120_Unwind_Reason_Code
121PERSONALITY_FUNCTION (int version,
122 _Unwind_Action actions,
123 _Unwind_Exception_Class exception_class ATTRIBUTE_UNUSED,
124 struct _Unwind_Exception *ue_header,
125 struct _Unwind_Context *context)
617a1b71 126#endif
0bfa5f65
RH
127{
128 lsda_header_info info;
f1ed99cd 129 const unsigned char *language_specific_data, *p;
0bfa5f65 130 _Unwind_Ptr landing_pad, ip;
754e45a8 131 int ip_before_insn = 0;
0bfa5f65 132
617a1b71 133#ifdef __ARM_EABI_UNWINDER__
74d9c39f 134 if ((state & _US_ACTION_MASK) != _US_UNWIND_FRAME_STARTING)
617a1b71
PB
135 CONTINUE_UNWINDING;
136
137 /* The dwarf unwinder assumes the context structure holds things like the
138 function and LSDA pointers. The ARM implementation caches these in
1e874273
PB
139 the exception header (UCB). To avoid rewriting everything we make a
140 virtual scratch register point at the UCB. */
617a1b71 141 ip = (_Unwind_Ptr) ue_header;
1e874273 142 _Unwind_SetGR (context, UNWIND_POINTER_REG, ip);
617a1b71 143#else
0bfa5f65
RH
144 if (version != 1)
145 return _URC_FATAL_PHASE1_ERROR;
146
147 /* Currently we only support cleanups for C. */
148 if ((actions & _UA_CLEANUP_PHASE) == 0)
617a1b71
PB
149 CONTINUE_UNWINDING;
150#endif
0bfa5f65
RH
151
152 language_specific_data = (const unsigned char *)
153 _Unwind_GetLanguageSpecificData (context);
154
155 /* If no LSDA, then there are no handlers or cleanups. */
156 if (! language_specific_data)
617a1b71 157 CONTINUE_UNWINDING;
0bfa5f65
RH
158
159 /* Parse the LSDA header. */
160 p = parse_lsda_header (context, language_specific_data, &info);
e64f5acf 161#ifdef HAVE_GETIPINFO
754e45a8 162 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
e64f5acf
SE
163#else
164 ip = _Unwind_GetIP (context);
165#endif
754e45a8
JJ
166 if (! ip_before_insn)
167 --ip;
0bfa5f65
RH
168 landing_pad = 0;
169
170#ifdef __USING_SJLJ_EXCEPTIONS__
171 /* The given "IP" is an index into the call-site table, with two
172 exceptions -- -1 means no-action, and 0 means terminate. But
173 since we're using uleb128 values, we've not got random access
174 to the array. */
175 if ((int) ip <= 0)
176 return _URC_CONTINUE_UNWIND;
177 else
178 {
f767122b 179 _uleb128_t cs_lp, cs_action;
0bfa5f65
RH
180 do
181 {
182 p = read_uleb128 (p, &cs_lp);
183 p = read_uleb128 (p, &cs_action);
184 }
185 while (--ip);
186
187 /* Can never have null landing pad for sjlj -- that would have
188 been indicated by a -1 call site index. */
f767122b 189 landing_pad = (_Unwind_Ptr)cs_lp + 1;
0bfa5f65
RH
190 goto found_something;
191 }
192#else
193 /* Search the call-site table for the action associated with this IP. */
194 while (p < info.action_table)
195 {
196 _Unwind_Ptr cs_start, cs_len, cs_lp;
f767122b 197 _uleb128_t cs_action;
0bfa5f65
RH
198
199 /* Note that all call-site encodings are "absolute" displacements. */
200 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
201 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
202 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
203 p = read_uleb128 (p, &cs_action);
204
205 /* The table is sorted, so if we've passed the ip, stop. */
206 if (ip < info.Start + cs_start)
207 p = info.action_table;
208 else if (ip < info.Start + cs_start + cs_len)
209 {
210 if (cs_lp)
211 landing_pad = info.LPStart + cs_lp;
0bfa5f65
RH
212 goto found_something;
213 }
214 }
0bfa5f65
RH
215#endif
216
217 /* IP is not in table. No associated cleanups. */
218 /* ??? This is where C++ calls std::terminate to catch throw
219 from a destructor. */
617a1b71 220 CONTINUE_UNWINDING;
0bfa5f65
RH
221
222 found_something:
223 if (landing_pad == 0)
224 {
225 /* IP is present, but has a null landing pad.
226 No handler to be run. */
617a1b71 227 CONTINUE_UNWINDING;
0bfa5f65
RH
228 }
229
230 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
231 (_Unwind_Ptr) ue_header);
232 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1), 0);
233 _Unwind_SetIP (context, landing_pad);
234 return _URC_INSTALL_CONTEXT;
235}
bf1431e3 236
e5a81c8e 237#if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
bf1431e3
TG
238EXCEPTION_DISPOSITION
239__gcc_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
240 PCONTEXT ms_orig_context, PDISPATCHER_CONTEXT ms_disp)
241{
242 return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
243 ms_disp, __gcc_personality_imp);
244}
245#endif /* SEH */