OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [exception.cc] - Blame information for rev 818

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 753 jeremybenn
// Functions for Exception Support for Java.
2
 
3
/* Copyright (C) 1998, 1999, 2001, 2002, 2006, 2010, 2011
4
   Free Software Foundation
5
 
6
   This file is part of libgcj.
7
 
8
This software is copyrighted work licensed under the terms of the
9
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
10
details.  */
11
 
12
#include <config.h>
13
 
14
#include <stddef.h>
15
#include <stdlib.h>
16
 
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>
22
 
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
27
{
28
  __attribute__ ((__noreturn__)) void
29
  abort ()
30
  {
31
    ::abort ();
32
  }
33
}
34
#include "unwind.h"
35
 
36
struct alignment_test_struct
37
{
38
  char space;
39
  char end[0] __attribute__((aligned));
40
};
41
 
42
struct java_exception_header
43
{
44
  /* Cache handler details between Phase 1 and Phase 2.  */
45
  _Unwind_Ptr landingPad;
46
  int handlerSwitchValue;
47
 
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)).  */
51
 
52
  char pad[sizeof(jthrowable) < sizeof(alignment_test_struct)
53
           ? sizeof(alignment_test_struct) - sizeof(jthrowable) : 0]
54
    __attribute__((aligned));
55
 
56
  jthrowable value;
57
 
58
  /* The generic exception header.  */
59
  _Unwind_Exception unwindHeader;
60
};
61
 
62
#ifdef __ARM_EABI_UNWINDER__
63
// This is the exception class we report -- "GNUCJAVA".
64
 
65
const _Unwind_Exception_Class __gcj_exception_class
66
  = {'G', 'N', 'U', 'C', 'J', 'A', 'V', 'A'};
67
 
68
static inline java_exception_header *
69
get_exception_header_from_ue (_Unwind_Exception *exc)
70
{
71
  return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
72
}
73
 
74
extern "C" void __cxa_begin_cleanup (_Unwind_Exception*);
75
 
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');
87
 
88
 
89
static inline java_exception_header *
90
get_exception_header_from_ue (_Unwind_Exception *exc)
91
{
92
  return reinterpret_cast<java_exception_header *>(exc + 1) - 1;
93
}
94
#endif // !__ARM_EABI_UNWINDER__
95
 
96
/* Perform a throw, Java style. Throw will unwind through this call,
97
   so there better not be any handlers or exception thrown here. */
98
 
99
extern "C" void
100
_Jv_Throw (jthrowable value)
101
{
102
  java_exception_header *xh
103
    = static_cast<java_exception_header *>(_Jv_AllocRawObj (sizeof (*xh)));
104
 
105
  if (value == NULL)
106
    value = new java::lang::NullPointerException ();
107
  xh->value = value;
108
 
109
  memcpy (&xh->unwindHeader.exception_class, &__gcj_exception_class,
110
          sizeof xh->unwindHeader.exception_class);
111
  xh->unwindHeader.exception_cleanup = NULL;
112
 
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
120
 
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();
129
}
130
 
131
 
132
#include "unwind-pe.h"
133
 
134
struct lsda_header_info
135
{
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;
142
};
143
 
144
static const unsigned char *
145
parse_lsda_header (_Unwind_Context *context, const unsigned char *p,
146
                   lsda_header_info *info)
147
{
148
  _uleb128_t tmp;
149
  unsigned char lpstart_encoding;
150
 
151
  info->Start = (context ? _Unwind_GetRegionStart (context) : 0);
152
 
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;
159
 
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)
163
    {
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;
171
    }
172
  else
173
    info->TType = 0;
174
 
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;
180
 
181
  return p;
182
}
183
 
184
static void **
185
get_ttype_entry (_Unwind_Context *context, lsda_header_info *info, long i)
186
{
187
  _Unwind_Ptr ptr;
188
 
189
  i *= size_of_encoded_value (info->ttype_encoding);
190
  read_encoded_value (context, info->ttype_encoding, info->TType - i, &ptr);
191
 
192
  return reinterpret_cast<void **>(ptr);
193
}
194
 
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
203
 
204
#ifdef __ARM_EABI_UNWINDER__
205
 
206
#define CONTINUE_UNWINDING \
207
  do                                                            \
208
    {                                                           \
209
      if (__gnu_unwind_frame(ue_header, context) != _URC_OK)    \
210
        return _URC_FAILURE;                                    \
211
      return _URC_CONTINUE_UNWIND;                              \
212
    }                                                           \
213
  while (0)
214
 
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
220
 
221
#define CONTINUE_UNWINDING return _URC_CONTINUE_UNWIND
222
 
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)
229
 
230
#endif
231
{
232
  java_exception_header *xh = get_exception_header_from_ue (ue_header);
233
 
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;
244
 
245
#ifdef __ARM_EABI_UNWINDER__
246
  _Unwind_Action actions;
247
 
248
  switch (state & _US_ACTION_MASK)
249
    {
250
    case _US_VIRTUAL_UNWIND_FRAME:
251
      actions = _UA_SEARCH_PHASE;
252
      break;
253
 
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;
260
 
261
    case _US_UNWIND_FRAME_RESUME:
262
      CONTINUE_UNWINDING;
263
      break;
264
 
265
    default:
266
      std::abort();
267
    }
268
  actions |= state & _US_FORCE_UNWIND;
269
 
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;
274
 
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);
281
 
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
288
 
289
  // Shortcut for phase 2 found handler for domestic exception.
290
  if (actions == (_UA_CLEANUP_PHASE | _UA_HANDLER_FRAME)
291
      && !foreign_exception)
292
    {
293
      handler_switch_value = xh->handlerSwitchValue;
294
      landing_pad = xh->landingPad;
295
      goto install_context;
296
    }
297
 
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.
306
 
307
  language_specific_data = (const unsigned char *)
308
    _Unwind_GetLanguageSpecificData (context);
309
 
310
  // If no LSDA, then there are no handlers or cleanups.
311
  if (! language_specific_data)
312
    CONTINUE_UNWINDING;
313
 
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;
326
 
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
335
    {
336
      _uleb128_t cs_lp, cs_action;
337
      do
338
        {
339
          p = read_uleb128 (p, &cs_lp);
340
          p = read_uleb128 (p, &cs_action);
341
        }
342
      while (--ip);
343
 
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;
350
    }
351
#else
352
  // Search the call-site table for the action associated with this IP.
353
  while (p < info.action_table)
354
    {
355
      _Unwind_Ptr cs_start, cs_len, cs_lp;
356
      _uleb128_t cs_action;
357
 
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);
363
 
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)
368
        {
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;
374
        }
375
    }
376
#endif // SJLJ_EXCEPTIONS
377
 
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;
382
 
383
 found_something:
384
  saw_cleanup = false;
385
  saw_handler = false;
386
 
387
  if (landing_pad == 0)
388
    {
389
      // If ip is present, and has a null landing pad, there are
390
      // no cleanups or handlers to be run.
391
    }
392
  else if (action_record == 0)
393
    {
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;
398
    }
399
  else
400
    {
401
      // Otherwise we have a catch handler.
402
      _sleb128_t ar_filter, ar_disp;
403
 
404
      while (1)
405
        {
406
          p = action_record;
407
          p = read_sleb128 (p, &ar_filter);
408
          read_sleb128 (p, &ar_disp);
409
 
410
          if (ar_filter == 0)
411
            {
412
              // Zero filter values are cleanups.
413
              saw_cleanup = true;
414
            }
415
 
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)
420
            ;
421
 
422
          else if (ar_filter > 0)
423
            {
424
              // Positive filter values are handlers.
425
 
426
              void **catch_word = get_ttype_entry (context, &info, ar_filter);
427
              jclass catch_type = (jclass)*catch_word;
428
 
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;
434
 
435
              if (_Jv_IsInstanceOf (xh->value, catch_type))
436
                {
437
                  handler_switch_value = ar_filter;
438
                  saw_handler = true;
439
                  break;
440
                }
441
            }
442
          else
443
            {
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 ();
450
            }
451
 
452
          if (ar_disp == 0)
453
            break;
454
          action_record = p + ar_disp;
455
        }
456
    }
457
 
458
  if (! saw_handler && ! saw_cleanup)
459
        CONTINUE_UNWINDING;
460
 
461
  if (actions & _UA_SEARCH_PHASE)
462
    {
463
      if (! saw_handler)
464
        CONTINUE_UNWINDING;
465
 
466
      // For domestic exceptions, we cache data from phase 1 for phase 2.
467
      if (! foreign_exception)
468
        {
469
          xh->handlerSwitchValue = handler_switch_value;
470
          xh->landingPad = landing_pad;
471
        }
472
      return _URC_HANDLER_FOUND;
473
    }
474
 
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;
486
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.