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
17 #include <java/lang/Class.h>
18 #include <java/lang/NullPointerException.h>
19 #include <gnu/gcj/RawData.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().
28 __attribute__ ((__noreturn__
)) void
36 struct alignment_test_struct
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
));
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. */
100 _Jv_Throw (jthrowable value
)
102 java_exception_header
*xh
103 = static_cast<java_exception_header
*>(_Jv_AllocRawObj (sizeof (*xh
)));
106 value
= new java::lang::NullPointerException ();
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
);
118 _Unwind_RaiseException (&xh
->unwindHeader
);
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
132 #include "unwind-pe.h"
134 struct lsda_header_info
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
)
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
);
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
;
169 p
= read_uleb128 (p
, &tmp
);
170 info
->TType
= p
+ tmp
;
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
;
185 get_ttype_entry (_Unwind_Context
*context
, lsda_header_info
*info
, long i
)
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 #elif defined (__SEH__)
201 #define PERSONALITY_FUNCTION __gcj_personality_imp
203 #define PERSONALITY_FUNCTION __gcj_personality_v0
206 #ifdef __ARM_EABI_UNWINDER__
208 #define CONTINUE_UNWINDING \
211 if (__gnu_unwind_frame(ue_header, context) != _URC_OK) \
212 return _URC_FAILURE; \
213 return _URC_CONTINUE_UNWIND; \
217 extern "C" _Unwind_Reason_Code
218 PERSONALITY_FUNCTION (_Unwind_State state
,
219 struct _Unwind_Exception
* ue_header
,
220 struct _Unwind_Context
* context
)
223 #define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
231 PERSONALITY_FUNCTION (int version
,
232 _Unwind_Action actions
,
233 _Unwind_Exception_Class exception_class
,
234 struct _Unwind_Exception
*ue_header
,
235 struct _Unwind_Context
*context
)
239 java_exception_header
*xh
= get_exception_header_from_ue (ue_header
);
241 lsda_header_info info
;
242 const unsigned char *language_specific_data
;
243 const unsigned char *action_record
;
244 const unsigned char *p
;
245 _Unwind_Ptr landing_pad
, ip
;
246 int handler_switch_value
;
249 bool foreign_exception
;
250 int ip_before_insn
= 0;
252 #ifdef __ARM_EABI_UNWINDER__
253 _Unwind_Action actions
;
255 switch (state
& _US_ACTION_MASK
)
257 case _US_VIRTUAL_UNWIND_FRAME
:
258 actions
= _UA_SEARCH_PHASE
;
261 case _US_UNWIND_FRAME_STARTING
:
262 actions
= _UA_CLEANUP_PHASE
;
263 if (!(state
& _US_FORCE_UNWIND
)
264 && ue_header
->barrier_cache
.sp
== _Unwind_GetGR(context
, 13))
265 actions
|= _UA_HANDLER_FRAME
;
268 case _US_UNWIND_FRAME_RESUME
:
275 actions
|= state
& _US_FORCE_UNWIND
;
277 // We don't know which runtime we're working with, so can't check this.
278 // However the ABI routines hide this from us, and we don't actually need
280 foreign_exception
= false;
282 // The dwarf unwinder assumes the context structure holds things like the
283 // function and LSDA pointers. The ARM implementation caches these in
284 // the exception header (UCB). To avoid rewriting everything we make the
285 // virtual IP register point at the UCB.
286 ip
= (_Unwind_Ptr
) ue_header
;
287 _Unwind_SetGR(context
, 12, ip
);
290 // Interface version check.
292 return _URC_FATAL_PHASE1_ERROR
;
293 foreign_exception
= exception_class
!= __gcj_exception_class
;
296 // Shortcut for phase 2 found handler for domestic exception.
297 if (actions
== (_UA_CLEANUP_PHASE
| _UA_HANDLER_FRAME
)
298 && !foreign_exception
)
300 handler_switch_value
= xh
->handlerSwitchValue
;
301 landing_pad
= xh
->landingPad
;
302 goto install_context
;
305 // FIXME: In Phase 1, record _Unwind_GetIPInfo in xh->obj as a part of
306 // the stack trace for this exception. This will only collect Java
307 // frames, but perhaps that is acceptable.
308 // FIXME2: _Unwind_GetIPInfo is nonsensical for SJLJ, being a call-site
309 // index instead of a PC value. We could perhaps arrange for
310 // _Unwind_GetRegionStart to return context->fc->jbuf[1], which
311 // is the address of the handler label for __builtin_longjmp, but
312 // there is no solution for DONT_USE_BUILTIN_SETJMP.
314 language_specific_data
= (const unsigned char *)
315 _Unwind_GetLanguageSpecificData (context
);
317 // If no LSDA, then there are no handlers or cleanups.
318 if (! language_specific_data
)
321 // Parse the LSDA header.
322 p
= parse_lsda_header (context
, language_specific_data
, &info
);
323 #ifdef HAVE_GETIPINFO
324 ip
= _Unwind_GetIPInfo (context
, &ip_before_insn
);
326 ip
= _Unwind_GetIP (context
);
328 if (! ip_before_insn
)
332 handler_switch_value
= 0;
334 #ifdef SJLJ_EXCEPTIONS
335 // The given "IP" is an index into the call-site table, with two
336 // exceptions -- -1 means no-action, and 0 means terminate. But
337 // since we're using uleb128 values, we've not got random access
340 return _URC_CONTINUE_UNWIND
;
343 _uleb128_t cs_lp
, cs_action
;
346 p
= read_uleb128 (p
, &cs_lp
);
347 p
= read_uleb128 (p
, &cs_action
);
351 // Can never have null landing pad for sjlj -- that would have
352 // been indicated by a -1 call site index.
353 landing_pad
= cs_lp
+ 1;
355 action_record
= info
.action_table
+ cs_action
- 1;
356 goto found_something
;
359 // Search the call-site table for the action associated with this IP.
360 while (p
< info
.action_table
)
362 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
363 _uleb128_t cs_action
;
365 // Note that all call-site encodings are "absolute" displacements.
366 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_start
);
367 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_len
);
368 p
= read_encoded_value (0, info
.call_site_encoding
, p
, &cs_lp
);
369 p
= read_uleb128 (p
, &cs_action
);
371 // The table is sorted, so if we've passed the ip, stop.
372 if (ip
< info
.Start
+ cs_start
)
373 p
= info
.action_table
;
374 else if (ip
< info
.Start
+ cs_start
+ cs_len
)
377 landing_pad
= info
.LPStart
+ cs_lp
;
379 action_record
= info
.action_table
+ cs_action
- 1;
380 goto found_something
;
383 #endif // SJLJ_EXCEPTIONS
385 // If ip is not present in the table, C++ would call terminate.
386 // ??? It is perhaps better to tweek the LSDA so that no-action
387 // is mapped to no-entry for Java.
394 if (landing_pad
== 0)
396 // If ip is present, and has a null landing pad, there are
397 // no cleanups or handlers to be run.
399 else if (action_record
== 0)
401 // If ip is present, has a non-null landing pad, and a null
402 // action table offset, then there are only cleanups present.
403 // Cleanups use a zero switch value, as set above.
408 // Otherwise we have a catch handler.
409 _sleb128_t ar_filter
, ar_disp
;
414 p
= read_sleb128 (p
, &ar_filter
);
415 read_sleb128 (p
, &ar_disp
);
419 // Zero filter values are cleanups.
423 // During forced unwinding, we only run cleanups. With a
424 // foreign exception class, we have no class info to match.
425 else if ((actions
& _UA_FORCE_UNWIND
)
426 || foreign_exception
)
429 else if (ar_filter
> 0)
431 // Positive filter values are handlers.
433 void **catch_word
= get_ttype_entry (context
, &info
, ar_filter
);
434 jclass catch_type
= (jclass
)*catch_word
;
436 // FIXME: This line is a kludge to work around exception
437 // handlers written in C++, which don't yet use indirect
439 if (catch_type
== *(void **)&java::lang::Class::class$
)
440 catch_type
= (jclass
)catch_word
;
442 if (_Jv_IsInstanceOf (xh
->value
, catch_type
))
444 handler_switch_value
= ar_filter
;
451 // Negative filter values are exception specifications,
452 // which Java does not use.
453 // ??? Perhaps better to make them an index into a table
454 // of null-terminated strings instead of playing games
455 // with Utf8Const+1 as above.
461 action_record
= p
+ ar_disp
;
465 if (! saw_handler
&& ! saw_cleanup
)
468 if (actions
& _UA_SEARCH_PHASE
)
473 // For domestic exceptions, we cache data from phase 1 for phase 2.
474 if (! foreign_exception
)
476 xh
->handlerSwitchValue
= handler_switch_value
;
477 xh
->landingPad
= landing_pad
;
479 return _URC_HANDLER_FOUND
;
483 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (0),
484 (_Unwind_Ptr
) &xh
->unwindHeader
);
485 _Unwind_SetGR (context
, __builtin_eh_return_data_regno (1),
486 handler_switch_value
);
487 _Unwind_SetIP (context
, landing_pad
);
488 #ifdef __ARM_EABI_UNWINDER__
490 __cxa_begin_cleanup(ue_header
);
492 return _URC_INSTALL_CONTEXT
;
497 EXCEPTION_DISPOSITION
498 __gcj_personality_seh0 (PEXCEPTION_RECORD ms_exc
, void *this_frame
,
499 PCONTEXT ms_orig_context
, PDISPATCHER_CONTEXT ms_disp
)
501 return _GCC_specific_handler (ms_exc
, this_frame
, ms_orig_context
,
502 ms_disp
, __gcj_personality_imp
);