* collect2.c (main): In AIX specific computations for vector
[official-gcc.git] / libjava / exception.cc
blob56f25ad44be5adb6198676bfa6c9743e4981fc07
1 // Functions for Exception Support for Java.
3 /* Copyright (C) 1998, 1999, 2001, 2002, 2006, 2010, 2011
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
12 #include <config.h>
14 #include <stddef.h>
15 #include <stdlib.h>
17 #include <java/lang/Class.h>
18 #include <java/lang/NullPointerException.h>
19 #include <gnu/gcj/RawData.h>
20 #include <gcj/cni.h>
21 #include <jvm.h>
23 // unwind-pe.h uses std::abort(), but sometimes we compile libjava
24 // without libstdc++-v3. The following hack forces it to use
25 // stdlib.h's abort().
26 namespace std
28 __attribute__ ((__noreturn__)) void
29 abort ()
31 ::abort ();
34 #include "unwind.h"
36 struct alignment_test_struct
38 char space;
39 char end[0] __attribute__((aligned));
42 struct java_exception_header
44 /* Cache handler details between Phase 1 and Phase 2. */
45 _Unwind_Ptr landingPad;
46 int handlerSwitchValue;
48 /* The object being thrown. Compiled code expects this to be immediately
49 before the generic exception header. Which is complicated by the fact
50 that _Unwind_Exception is ((aligned)). */
52 char pad[sizeof(jthrowable) < sizeof(alignment_test_struct)
53 ? sizeof(alignment_test_struct) - sizeof(jthrowable) : 0]
54 __attribute__((aligned));
56 jthrowable value;
58 /* The generic exception header. */
59 _Unwind_Exception unwindHeader;
62 #ifdef __ARM_EABI_UNWINDER__
63 // This is the exception class we report -- "GNUCJAVA".
65 const _Unwind_Exception_Class __gcj_exception_class
66 = {'G', 'N', 'U', 'C', 'J', 'A', 'V', 'A'};
68 static inline java_exception_header *
69 get_exception_header_from_ue (_Unwind_Exception *exc)
71 return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
74 extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
76 #else // !__ARM_EABI_UNWINDER__
77 // This is the exception class we report -- "GNUCJAVA".
78 const _Unwind_Exception_Class __gcj_exception_class
79 = ((((((((_Unwind_Exception_Class) 'G'
80 << 8 | (_Unwind_Exception_Class) 'N')
81 << 8 | (_Unwind_Exception_Class) 'U')
82 << 8 | (_Unwind_Exception_Class) 'C')
83 << 8 | (_Unwind_Exception_Class) 'J')
84 << 8 | (_Unwind_Exception_Class) 'A')
85 << 8 | (_Unwind_Exception_Class) 'V')
86 << 8 | (_Unwind_Exception_Class) 'A');
89 static inline java_exception_header *
90 get_exception_header_from_ue (_Unwind_Exception *exc)
92 return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
94 #endif // !__ARM_EABI_UNWINDER__
96 /* Perform a throw, Java style. Throw will unwind through this call,
97 so there better not be any handlers or exception thrown here. */
99 extern "C" void
100 _Jv_Throw (jthrowable value)
102 java_exception_header *xh
103 = static_cast<java_exception_header *>(_Jv_AllocRawObj (sizeof (*xh)));
105 if (value == NULL)
106 value = new java::lang::NullPointerException ();
107 xh->value = value;
109 memcpy (&xh->unwindHeader.exception_class, &__gcj_exception_class,
110 sizeof xh->unwindHeader.exception_class);
111 xh->unwindHeader.exception_cleanup = NULL;
113 /* We're happy with setjmp/longjmp exceptions or region-based
114 exception handlers: entry points are provided here for both. */
115 #ifdef SJLJ_EXCEPTIONS
116 _Unwind_SjLj_RaiseException (&xh->unwindHeader);
117 #else
118 _Unwind_RaiseException (&xh->unwindHeader);
119 #endif
121 /* If code == _URC_END_OF_STACK, then we reached top of stack without
122 finding a handler for the exception. Since each thread is run in
123 a try/catch, this oughtn't happen. If code is something else, we
124 encountered some sort of heinous lossage from which we could not
125 recover. As is the way of such things, almost certainly we will have
126 crashed before now, rather than actually being able to diagnose the
127 problem. */
128 abort();
132 #include "unwind-pe.h"
134 struct lsda_header_info
136 _Unwind_Ptr Start;
137 _Unwind_Ptr LPStart;
138 const unsigned char *TType;
139 const unsigned char *action_table;
140 unsigned char ttype_encoding;
141 unsigned char call_site_encoding;
144 static const unsigned char *
145 parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
146 lsda_header_info *info)
148 _uleb128_t tmp;
149 unsigned char lpstart_encoding;
151 info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
153 // Find @LPStart, the base to which landing pad offsets are relative.
154 lpstart_encoding = *p++;
155 if (lpstart_encoding != DW_EH_PE_omit)
156 p = read_encoded_value (context, lpstart_encoding, p, &info->LPStart);
157 else
158 info->LPStart = info->Start;
160 // Find @TType, the base of the handler and exception spec type data.
161 info->ttype_encoding = *p++;
162 if (info->ttype_encoding != DW_EH_PE_omit)
164 #if _GLIBCXX_OVERRIDE_TTYPE_ENCODING
165 /* Older ARM EABI toolchains set this value incorrectly, so use a
166 hardcoded OS-specific format. */
167 info->ttype_encoding = _GLIBCXX_OVERRIDE_TTYPE_ENCODING;
168 #endif
169 p = read_uleb128 (p, &tmp);
170 info->TType = p + tmp;
172 else
173 info->TType = 0;
175 // The encoding and length of the call-site table; the action table
176 // immediately follows.
177 info->call_site_encoding = *p++;
178 p = read_uleb128 (p, &tmp);
179 info->action_table = p + tmp;
181 return p;
184 static void **
185 get_ttype_entry (_Unwind_Context *context, lsda_header_info *info, long i)
187 _Unwind_Ptr ptr;
189 i *= size_of_encoded_value (info->ttype_encoding);
190 read_encoded_value (context, info->ttype_encoding, info->TType - i, &ptr);
192 return reinterpret_cast<void **>(ptr);
195 // Using a different personality function name causes link failures
196 // when trying to mix code using different exception handling models.
197 #ifdef SJLJ_EXCEPTIONS
198 #define PERSONALITY_FUNCTION __gcj_personality_sj0
199 #define __builtin_eh_return_data_regno(x) x
200 #else
201 #define PERSONALITY_FUNCTION __gcj_personality_v0
202 #endif
204 #ifdef __ARM_EABI_UNWINDER__
206 #define CONTINUE_UNWINDING \
207 do \
209 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
210 return _URC_FAILURE; \
211 return _URC_CONTINUE_UNWIND; \
213 while (0)
215 extern "C" _Unwind_Reason_Code
216 PERSONALITY_FUNCTION (_Unwind_State state,
217 struct _Unwind_Exception* ue_header,
218 struct _Unwind_Context* context)
219 #else
221 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
223 extern "C" _Unwind_Reason_Code
224 PERSONALITY_FUNCTION (int version,
225 _Unwind_Action actions,
226 _Unwind_Exception_Class exception_class,
227 struct _Unwind_Exception *ue_header,
228 struct _Unwind_Context *context)
230 #endif
232 java_exception_header *xh = get_exception_header_from_ue (ue_header);
234 lsda_header_info info;
235 const unsigned char *language_specific_data;
236 const unsigned char *action_record;
237 const unsigned char *p;
238 _Unwind_Ptr landing_pad, ip;
239 int handler_switch_value;
240 bool saw_cleanup;
241 bool saw_handler;
242 bool foreign_exception;
243 int ip_before_insn = 0;
245 #ifdef __ARM_EABI_UNWINDER__
246 _Unwind_Action actions;
248 switch (state & _US_ACTION_MASK)
250 case _US_VIRTUAL_UNWIND_FRAME:
251 actions = _UA_SEARCH_PHASE;
252 break;
254 case _US_UNWIND_FRAME_STARTING:
255 actions = _UA_CLEANUP_PHASE;
256 if (!(state & _US_FORCE_UNWIND)
257 && ue_header->barrier_cache.sp == _Unwind_GetGR(context, 13))
258 actions |= _UA_HANDLER_FRAME;
259 break;
261 case _US_UNWIND_FRAME_RESUME:
262 CONTINUE_UNWINDING;
263 break;
265 default:
266 std::abort();
268 actions |= state & _US_FORCE_UNWIND;
270 // We don't know which runtime we're working with, so can't check this.
271 // However the ABI routines hide this from us, and we don't actually need
272 // to know.
273 foreign_exception = false;
275 // The dwarf unwinder assumes the context structure holds things like the
276 // function and LSDA pointers. The ARM implementation caches these in
277 // the exception header (UCB). To avoid rewriting everything we make the
278 // virtual IP register point at the UCB.
279 ip = (_Unwind_Ptr) ue_header;
280 _Unwind_SetGR(context, 12, ip);
282 #else
283 // Interface version check.
284 if (version != 1)
285 return _URC_FATAL_PHASE1_ERROR;
286 foreign_exception = exception_class != __gcj_exception_class;
287 #endif
289 // Shortcut for phase 2 found handler for domestic exception.
290 if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
291 && !foreign_exception)
293 handler_switch_value = xh->handlerSwitchValue;
294 landing_pad = xh->landingPad;
295 goto install_context;
298 // FIXME: In Phase 1, record _Unwind_GetIPInfo in xh->obj as a part of
299 // the stack trace for this exception. This will only collect Java
300 // frames, but perhaps that is acceptable.
301 // FIXME2: _Unwind_GetIPInfo is nonsensical for SJLJ, being a call-site
302 // index instead of a PC value. We could perhaps arrange for
303 // _Unwind_GetRegionStart to return context->fc->jbuf[1], which
304 // is the address of the handler label for __builtin_longjmp, but
305 // there is no solution for DONT_USE_BUILTIN_SETJMP.
307 language_specific_data = (const unsigned char *)
308 _Unwind_GetLanguageSpecificData (context);
310 // If no LSDA, then there are no handlers or cleanups.
311 if (! language_specific_data)
312 CONTINUE_UNWINDING;
314 // Parse the LSDA header.
315 p = parse_lsda_header (context, language_specific_data, &info);
316 #ifdef HAVE_GETIPINFO
317 ip = _Unwind_GetIPInfo (context, &ip_before_insn);
318 #else
319 ip = _Unwind_GetIP (context);
320 #endif
321 if (! ip_before_insn)
322 --ip;
323 landing_pad = 0;
324 action_record = 0;
325 handler_switch_value = 0;
327 #ifdef SJLJ_EXCEPTIONS
328 // The given "IP" is an index into the call-site table, with two
329 // exceptions -- -1 means no-action, and 0 means terminate. But
330 // since we're using uleb128 values, we've not got random access
331 // to the array.
332 if ((int) ip <= 0)
333 return _URC_CONTINUE_UNWIND;
334 else
336 _uleb128_t cs_lp, cs_action;
339 p = read_uleb128 (p, &cs_lp);
340 p = read_uleb128 (p, &cs_action);
342 while (--ip);
344 // Can never have null landing pad for sjlj -- that would have
345 // been indicated by a -1 call site index.
346 landing_pad = cs_lp + 1;
347 if (cs_action)
348 action_record = info.action_table + cs_action - 1;
349 goto found_something;
351 #else
352 // Search the call-site table for the action associated with this IP.
353 while (p < info.action_table)
355 _Unwind_Ptr cs_start, cs_len, cs_lp;
356 _uleb128_t cs_action;
358 // Note that all call-site encodings are "absolute" displacements.
359 p = read_encoded_value (0, info.call_site_encoding, p, &cs_start);
360 p = read_encoded_value (0, info.call_site_encoding, p, &cs_len);
361 p = read_encoded_value (0, info.call_site_encoding, p, &cs_lp);
362 p = read_uleb128 (p, &cs_action);
364 // The table is sorted, so if we've passed the ip, stop.
365 if (ip < info.Start + cs_start)
366 p = info.action_table;
367 else if (ip < info.Start + cs_start + cs_len)
369 if (cs_lp)
370 landing_pad = info.LPStart + cs_lp;
371 if (cs_action)
372 action_record = info.action_table + cs_action - 1;
373 goto found_something;
376 #endif // SJLJ_EXCEPTIONS
378 // If ip is not present in the table, C++ would call terminate.
379 // ??? It is perhaps better to tweek the LSDA so that no-action
380 // is mapped to no-entry for Java.
381 CONTINUE_UNWINDING;
383 found_something:
384 saw_cleanup = false;
385 saw_handler = false;
387 if (landing_pad == 0)
389 // If ip is present, and has a null landing pad, there are
390 // no cleanups or handlers to be run.
392 else if (action_record == 0)
394 // If ip is present, has a non-null landing pad, and a null
395 // action table offset, then there are only cleanups present.
396 // Cleanups use a zero switch value, as set above.
397 saw_cleanup = true;
399 else
401 // Otherwise we have a catch handler.
402 _sleb128_t ar_filter, ar_disp;
404 while (1)
406 p = action_record;
407 p = read_sleb128 (p, &ar_filter);
408 read_sleb128 (p, &ar_disp);
410 if (ar_filter == 0)
412 // Zero filter values are cleanups.
413 saw_cleanup = true;
416 // During forced unwinding, we only run cleanups. With a
417 // foreign exception class, we have no class info to match.
418 else if ((actions & _UA_FORCE_UNWIND)
419 || foreign_exception)
422 else if (ar_filter > 0)
424 // Positive filter values are handlers.
426 void **catch_word = get_ttype_entry (context, &info, ar_filter);
427 jclass catch_type = (jclass)*catch_word;
429 // FIXME: This line is a kludge to work around exception
430 // handlers written in C++, which don't yet use indirect
431 // dispatch.
432 if (catch_type == *(void **)&java::lang::Class::class$)
433 catch_type = (jclass)catch_word;
435 if (_Jv_IsInstanceOf (xh->value, catch_type))
437 handler_switch_value = ar_filter;
438 saw_handler = true;
439 break;
442 else
444 // Negative filter values are exception specifications,
445 // which Java does not use.
446 // ??? Perhaps better to make them an index into a table
447 // of null-terminated strings instead of playing games
448 // with Utf8Const+1 as above.
449 abort ();
452 if (ar_disp == 0)
453 break;
454 action_record = p + ar_disp;
458 if (! saw_handler && ! saw_cleanup)
459 CONTINUE_UNWINDING;
461 if (actions & _UA_SEARCH_PHASE)
463 if (! saw_handler)
464 CONTINUE_UNWINDING;
466 // For domestic exceptions, we cache data from phase 1 for phase 2.
467 if (! foreign_exception)
469 xh->handlerSwitchValue = handler_switch_value;
470 xh->landingPad = landing_pad;
472 return _URC_HANDLER_FOUND;
475 install_context:
476 _Unwind_SetGR (context, __builtin_eh_return_data_regno (0),
477 (_Unwind_Ptr) &xh->unwindHeader);
478 _Unwind_SetGR (context, __builtin_eh_return_data_regno (1),
479 handler_switch_value);
480 _Unwind_SetIP (context, landing_pad);
481 #ifdef __ARM_EABI_UNWINDER__
482 if (saw_cleanup)
483 __cxa_begin_cleanup(ue_header);
484 #endif
485 return _URC_INSTALL_CONTEXT;