os/kernelhwsrv/kernel/eka/compsupp/aehabi/cppsemantics.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* The C++ exceptions runtime support
sl@0
     2
 *
sl@0
     3
 * Copyright 2002-2003 ARM Limited.
sl@0
     4
 */
sl@0
     5
/*
sl@0
     6
  Licence
sl@0
     7
sl@0
     8
  1. Subject to the provisions of clause 2, ARM hereby grants to LICENSEE a
sl@0
     9
  perpetual, non-exclusive, nontransferable, royalty free, worldwide licence
sl@0
    10
  to use this Example Implementation of Exception Handling solely for the
sl@0
    11
  purpose of developing, having developed, manufacturing, having
sl@0
    12
  manufactured, offering to sell, selling, supplying or otherwise
sl@0
    13
  distributing products which comply with the Exception Handling ABI for the
sl@0
    14
  ARM Architecture specification. All other rights are reserved to ARM or its
sl@0
    15
  licensors.
sl@0
    16
sl@0
    17
  2. THIS EXAMPLE IMPLEMENTATION OF EXCEPTION HANDLING  IS PROVIDED "AS IS"
sl@0
    18
  WITH NO WARRANTIES EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED
sl@0
    19
  TO ANY WARRANTY OF SATISFACTORY QUALITY, MERCHANTABILITY, NONINFRINGEMENT
sl@0
    20
  OR FITNESS FOR A PARTICULAR PURPOSE.
sl@0
    21
*/
sl@0
    22
/*
sl@0
    23
 * RCS $Revision: 1.29.2.1 $
sl@0
    24
 * Checkin $Date: 2004/01/20 15:11:16 $
sl@0
    25
 * Revising $Author: achapman $
sl@0
    26
 */
sl@0
    27
sl@0
    28
/* This source file is compiled automatically by ARM's make system into
sl@0
    29
 * multiple object files. The source regions constituting object file
sl@0
    30
 * xxx.o are delimited by ifdef xxx_c / endif directives.
sl@0
    31
 *
sl@0
    32
 * The source regions currently marked are:
sl@0
    33
 * arm_exceptions_globs_c
sl@0
    34
 * arm_exceptions_mem_c
sl@0
    35
 * arm_exceptions_uncaught_c
sl@0
    36
 * arm_exceptions_terminate_c
sl@0
    37
 * arm_exceptions_setterminate_c
sl@0
    38
 * arm_exceptions_unexpected_c
sl@0
    39
 * arm_exceptions_setunexpected_c
sl@0
    40
 * arm_exceptions_support_c
sl@0
    41
 * arm_exceptions_callterm_c
sl@0
    42
 * arm_exceptions_callunex_c
sl@0
    43
 * arm_exceptions_currenttype_c
sl@0
    44
 * arm_exceptions_alloc_c
sl@0
    45
 * arm_exceptions_free_c
sl@0
    46
 * arm_exceptions_throw_c
sl@0
    47
 * arm_exceptions_rethrow_c
sl@0
    48
 * arm_exceptions_foreign_c
sl@0
    49
 * arm_exceptions_cleanup_c
sl@0
    50
 * arm_exceptions_begincatch_c
sl@0
    51
 * arm_exceptions_endcatch_c
sl@0
    52
 * arm_exceptions_bad_typeid_c
sl@0
    53
 * arm_exceptions_bad_cast_c
sl@0
    54
 */
sl@0
    55
sl@0
    56
#include <string.h>
sl@0
    57
#include <typeinfo>
sl@0
    58
#include <exception>
sl@0
    59
#include <new>
sl@0
    60
// Environment:
sl@0
    61
#include "unwind_env.h"
sl@0
    62
// Language-independent unwinder declarations:
sl@0
    63
#include "unwinder.h"
sl@0
    64
sl@0
    65
/* By default, none of these routines are unwindable: */
sl@0
    66
#pragma noexceptions_unwind
sl@0
    67
sl@0
    68
/* For brevity: */
sl@0
    69
sl@0
    70
typedef _Unwind_Control_Block UCB;
sl@0
    71
sl@0
    72
using std::terminate_handler;
sl@0
    73
using std::unexpected_handler;
sl@0
    74
using std::terminate;
sl@0
    75
using std::unexpected;
sl@0
    76
using std::type_info;
sl@0
    77
sl@0
    78
/* Redeclare these interface routines as weak, so using them does not
sl@0
    79
 * pull in the unwind library. We only want the unwind library if
sl@0
    80
 * someone throws (or raises an exception from some other language).
sl@0
    81
 */
sl@0
    82
WEAKDECL NORETURNDECL void _Unwind_Resume(UCB *);
sl@0
    83
WEAKDECL void _Unwind_Complete(UCB *);
sl@0
    84
sl@0
    85
/* Diagnostics:
sl@0
    86
 * Define DEBUG to get extra interfaces which assist debugging this functionality.
sl@0
    87
 * Define PRINTED_DIAGNOSTICS for printed diagnostics.
sl@0
    88
 */
sl@0
    89
#ifdef DEBUG
sl@0
    90
#define PRINTED_DIAGNOSTICS
sl@0
    91
#endif
sl@0
    92
sl@0
    93
#ifdef PRINTED_DIAGNOSTICS
sl@0
    94
extern "C" int printf(const char *, ...);
sl@0
    95
#endif
sl@0
    96
sl@0
    97
/* --------- "Exceptions_class" string for our implementation: --------- */
sl@0
    98
sl@0
    99
#define EXCEPTIONS_CLASS_SIZE 8
sl@0
   100
#define ARMCPP_EXCEPTIONS_CLASS "ARM\0C++\0"
sl@0
   101
sl@0
   102
sl@0
   103
/* --------- Exception control object: --------- */
sl@0
   104
sl@0
   105
// Type __cxa_exception is the combined C++ housekeeping (LEO) and UCB.
sl@0
   106
// It will be followed by the user exception object, hence must ensure
sl@0
   107
// the latter is aligned on an 8 byte boundary.
sl@0
   108
sl@0
   109
struct __cxa_exception {
sl@0
   110
  const type_info *exceptionType;       // RTTI object describing the type of the exception
sl@0
   111
  void *(*exceptionDestructor)(void *); // Destructor for the exception object (may be NULL)
sl@0
   112
  unexpected_handler unexpectedHandler; // Handler in force after evaluating throw expr
sl@0
   113
  terminate_handler terminateHandler;   // Handler in force after evaluating throw expr
sl@0
   114
  __cxa_exception *nextCaughtException; // Chain of "currently caught" c++ exception objects
sl@0
   115
  uint32_t handlerCount;                // Count of how many handlers this EO is "caught" in
sl@0
   116
  __cxa_exception *nextPropagatingException; // Chain of objects saved over cleanup
sl@0
   117
  uint32_t propagationCount;            // Count of live propagations (throws) of this EO
sl@0
   118
  UCB ucb;                              // Forces alignment of next item to 8-byte boundary
sl@0
   119
};
sl@0
   120
sl@0
   121
sl@0
   122
/* --------- Control "globals": --------- */
sl@0
   123
sl@0
   124
// We do this by putting all the thread-specific "globals" into a single
sl@0
   125
// area of store, which we allocate space for dynamically.
sl@0
   126
// We don't define a constructor for this; see comments with __cxa_get_globals.
sl@0
   127
sl@0
   128
typedef void (*handler)(void);
sl@0
   129
sl@0
   130
struct __cxa_eh_globals {
sl@0
   131
  uint32_t uncaughtExceptions;               // counter
sl@0
   132
  unexpected_handler unexpectedHandler;      // per-thread handler
sl@0
   133
  terminate_handler terminateHandler;        // per-thread handler
sl@0
   134
  bool implementation_ever_called_terminate; // true if it ever did
sl@0
   135
  handler call_hook;     // transient field to tell terminate/unexpected which hook to call
sl@0
   136
  __cxa_exception *caughtExceptions;         // chain of "caught" exceptions
sl@0
   137
  __cxa_exception *propagatingExceptions;    // chain of "propagating" (in cleanup) exceptions
sl@0
   138
  void *emergency_buffer;                    // emergency buffer for when rest of heap full
sl@0
   139
};
sl@0
   140
sl@0
   141
sl@0
   142
/* ---------- Entry points: ---------- */
sl@0
   143
sl@0
   144
/* There is a little type-delicacy required here as __cxa_throw takes a
sl@0
   145
 * function pointer. Setting aside the problem of not being able to form
sl@0
   146
 * a pointer to a destructor in C++, if we simply say extern "C" here
sl@0
   147
 * then the function pointer will also have C linkage and will be a
sl@0
   148
 * pointer to a C function. This causes problems when __cxa_throw is
sl@0
   149
 * defined (unless we repeat the extern "C" at the definition site) because
sl@0
   150
 * the fnptr in the definition gets C++ linkage, hence that __cxa_throw has
sl@0
   151
 * a different signature to the declared one, and so the function we wanted
sl@0
   152
 * doesn't get defined at all.
sl@0
   153
 * Maybe it should just take a void * but this seems more honest.
sl@0
   154
 */
sl@0
   155
sl@0
   156
typedef void *(*cppdtorptr)(void *);
sl@0
   157
sl@0
   158
extern "C" {
sl@0
   159
sl@0
   160
  // Protocol routines called directly from application code
sl@0
   161
sl@0
   162
  void *__cxa_allocate_exception(size_t size);
sl@0
   163
  void __cxa_free_exception(void *);
sl@0
   164
  WEAKDECL void __cxa_throw(void *, const type_info *, cppdtorptr);
sl@0
   165
  void __cxa_rethrow(void);
sl@0
   166
  void *__cxa_begin_catch(UCB *);
sl@0
   167
  void __cxa_end_catch(void);
sl@0
   168
  void __cxa_end_cleanup(void);
sl@0
   169
  const type_info *__cxa_current_exception_type(void);
sl@0
   170
sl@0
   171
  // Protocol routines usually called only by the personality routine(s).
sl@0
   172
sl@0
   173
  void __cxa_call_terminate(UCB *);
sl@0
   174
  void __cxa_call_unexpected(UCB *);
sl@0
   175
  bool __cxa_begin_cleanup(UCB *);
sl@0
   176
  bool __cxa_type_match(UCB *, const std::type_info *, void **);
sl@0
   177
sl@0
   178
  // Auxilliary routines
sl@0
   179
sl@0
   180
  __cxa_eh_globals *__cxa_get_globals(void);
sl@0
   181
  void __cxa_bad_typeid(void);
sl@0
   182
  void __cxa_bad_cast(void);
sl@0
   183
sl@0
   184
  // Emergency memory buffer management routines
sl@0
   185
sl@0
   186
  void *__ARM_exceptions_buffer_init(void);
sl@0
   187
  void *__ARM_exceptions_buffer_allocate(void *, size_t);
sl@0
   188
  void *__ARM_exceptions_buffer_free(void *, void *);
sl@0
   189
}
sl@0
   190
sl@0
   191
sl@0
   192
// Support routines
sl@0
   193
sl@0
   194
#define NAMES __ARM
sl@0
   195
namespace NAMES {
sl@0
   196
  void default_unexpected_handler(void);
sl@0
   197
  void call_terminate_handler(UCB *);
sl@0
   198
  void eh_catch_semantics(UCB *);
sl@0
   199
  bool is_foreign_exception(UCB *);
sl@0
   200
  bool same_exceptions_class(const void *, const void *);
sl@0
   201
  __cxa_exception *get_foreign_intermediary(__cxa_exception *, UCB *);
sl@0
   202
}
sl@0
   203
sl@0
   204
// Macro: convert ucb pointer to __cxa_exception pointer
sl@0
   205
sl@0
   206
#define ucbp_to_ep(UCB_P) ((__cxa_exception *)((char *)(UCB_P) - offsetof(__cxa_exception, ucb)))
sl@0
   207
sl@0
   208
sl@0
   209
#ifdef arm_exceptions_globs_c
sl@0
   210
sl@0
   211
/* --------- Allocating and retrieving "globals": --------- */
sl@0
   212
sl@0
   213
// The exception-handling globals should be allocated per-thread.
sl@0
   214
// This is done here assuming the existance of a zero-initialised void*
sl@0
   215
// pointer location obtainable by the macro EH_GLOBALS.
sl@0
   216
sl@0
   217
// Default terminate handler:
sl@0
   218
sl@0
   219
static void __default_terminate_handler(void) {
sl@0
   220
  abort();
sl@0
   221
}
sl@0
   222
sl@0
   223
// If std::unexpected() is in the image, include a default handler for it:
sl@0
   224
namespace NAMES { WEAKDECL void default_unexpected_handler(void); }
sl@0
   225
sl@0
   226
// If this symbol is present, allocate an emergency buffer.
sl@0
   227
// As we aren't allowed static data, make it a function
sl@0
   228
extern "C" WEAKDECL void __ARM_exceptions_buffer_required(void);
sl@0
   229
sl@0
   230
sl@0
   231
// __cxa_eh_globals returns the per-thread memory. There are several complications,
sl@0
   232
// all of which relate to not touching the exceptions system while trying to
sl@0
   233
// initialise it:
sl@0
   234
// 1) We can't obtain memory by calling new or nothrow new as both of these use
sl@0
   235
//    exceptions internally, so we must use malloc
sl@0
   236
// 2) We choose not to initialise the memory via placement new and a constructor,
sl@0
   237
//    since placement new is declared with an empty function exception specification,
sl@0
   238
//    which causes more of the exceptions system to always be pulled in.
sl@0
   239
// 3) We can't call terminate, as terminate looks in the memory we are trying to
sl@0
   240
//    allocate.
sl@0
   241
sl@0
   242
__cxa_eh_globals *__cxa_get_globals(void)
sl@0
   243
{
sl@0
   244
  __cxa_eh_globals *this_thread_globals = (__cxa_eh_globals *)(EH_GLOBALS);
sl@0
   245
  if (this_thread_globals == NULL) {
sl@0
   246
sl@0
   247
    // First call
sl@0
   248
    // Obtain some memory: this is thread-safe provided malloc is.
sl@0
   249
    this_thread_globals = (__cxa_eh_globals *)malloc(sizeof(__cxa_eh_globals));
sl@0
   250
    if (this_thread_globals == NULL) abort(); // NOT terminate(), which calls this fn
sl@0
   251
sl@0
   252
    // Save the pointer in the specially-provided location
sl@0
   253
    EH_GLOBALS = this_thread_globals;
sl@0
   254
sl@0
   255
    // Finally initialise the memory by hand
sl@0
   256
    this_thread_globals->uncaughtExceptions = 0;
sl@0
   257
    this_thread_globals->unexpectedHandler = NAMES::default_unexpected_handler;
sl@0
   258
    this_thread_globals->terminateHandler = __default_terminate_handler;
sl@0
   259
    this_thread_globals->implementation_ever_called_terminate = false;
sl@0
   260
    this_thread_globals->call_hook = NULL;
sl@0
   261
    this_thread_globals->caughtExceptions = NULL;
sl@0
   262
    this_thread_globals->propagatingExceptions = NULL;
sl@0
   263
    if (&__ARM_exceptions_buffer_required == NULL)
sl@0
   264
      this_thread_globals->emergency_buffer = NULL;
sl@0
   265
    else
sl@0
   266
      this_thread_globals->emergency_buffer = __ARM_exceptions_buffer_init();
sl@0
   267
  }
sl@0
   268
sl@0
   269
  return this_thread_globals;
sl@0
   270
}
sl@0
   271
sl@0
   272
sl@0
   273
#endif /* arm_exceptions_globs_c */
sl@0
   274
#ifdef arm_exceptions_mem_c
sl@0
   275
sl@0
   276
/* --------- Emergency memory: --------- */
sl@0
   277
sl@0
   278
// It is possible to reserve memory for throwing bad_alloc when the heap
sl@0
   279
// is otherwise full. The ARM implementation provides hooks to do this.
sl@0
   280
// The default implementation reserves just enough space for a bad_alloc
sl@0
   281
// object, so if memory is later exhausted bad_alloc can still be thrown.
sl@0
   282
// Note there is no guarantee or requirement that the exception being
sl@0
   283
// thrown is actually bad_alloc.
sl@0
   284
sl@0
   285
// A usage flag and enough space for a bad_alloc exception control object
sl@0
   286
sl@0
   287
struct emergency_eco {
sl@0
   288
  __cxa_exception ep;
sl@0
   289
  std::bad_alloc b;
sl@0
   290
};
sl@0
   291
sl@0
   292
struct emergency_buffer {
sl@0
   293
  bool inuse;
sl@0
   294
  struct emergency_eco eco;
sl@0
   295
};
sl@0
   296
sl@0
   297
// Initialiser
sl@0
   298
void* __ARM_exceptions_buffer_init(void)
sl@0
   299
{
sl@0
   300
  emergency_buffer *buffer = (emergency_buffer *)malloc(sizeof(emergency_buffer));
sl@0
   301
  if (buffer == NULL) return NULL;
sl@0
   302
  buffer->inuse = false;
sl@0
   303
  return buffer;
sl@0
   304
}
sl@0
   305
sl@0
   306
// Allocator
sl@0
   307
void *__ARM_exceptions_buffer_allocate(void *buffer, size_t size)
sl@0
   308
{
sl@0
   309
  emergency_buffer *b = (emergency_buffer *)buffer;
sl@0
   310
  if (size > sizeof(emergency_eco) || b == NULL || b->inuse) return NULL;
sl@0
   311
  b->inuse = true;
sl@0
   312
  return &b->eco;
sl@0
   313
}
sl@0
   314
sl@0
   315
// Deallocator: Must return non-NULL if and only if it recognises
sl@0
   316
// and releases the supplied object
sl@0
   317
void *__ARM_exceptions_buffer_free(void *buffer, void *addr)
sl@0
   318
{
sl@0
   319
  emergency_buffer *b = (emergency_buffer *)buffer;
sl@0
   320
  if (b == NULL || addr != &b->eco) return NULL;
sl@0
   321
  b->inuse = false;
sl@0
   322
  return b;
sl@0
   323
}
sl@0
   324
sl@0
   325
sl@0
   326
#endif /* arm_exceptions_mem_c */
sl@0
   327
#ifdef arm_exceptions_uncaught_c
sl@0
   328
sl@0
   329
/* ---- uncaught_exception() ---- */
sl@0
   330
sl@0
   331
/* The EDG (and I think our) interpretation is that if the implementation
sl@0
   332
 * ever called terminate(), uncaught_exception() should return true.
sl@0
   333
 */
sl@0
   334
sl@0
   335
bool std::uncaught_exception(void)
sl@0
   336
{
sl@0
   337
   __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   338
   return g->implementation_ever_called_terminate || g->uncaughtExceptions;
sl@0
   339
}
sl@0
   340
sl@0
   341
sl@0
   342
#endif /* arm_exceptions_uncaught_c */
sl@0
   343
#ifdef arm_exceptions_terminate_c
sl@0
   344
sl@0
   345
/* ---- terminate() etc ---- */
sl@0
   346
sl@0
   347
/* The behaviour of terminate() must differ between calls by the
sl@0
   348
 * implementation and calls by the application. This is achieved by having the
sl@0
   349
 * implementation set call_hook immediately before the call to terminate().
sl@0
   350
 * The hook called by terminate() should terminate the program without
sl@0
   351
 * returning to the caller. There is no requirement for terminate() itself to
sl@0
   352
 * intercept throws.
sl@0
   353
 */
sl@0
   354
sl@0
   355
void std::terminate(void)
sl@0
   356
{
sl@0
   357
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   358
sl@0
   359
  if (g->call_hook != NULL) {
sl@0
   360
    // Clear then call hook fn we were passed
sl@0
   361
    handler call_hook = g->call_hook;
sl@0
   362
    g->call_hook = NULL;
sl@0
   363
    call_hook();
sl@0
   364
  } else {
sl@0
   365
    // Call global hook fn
sl@0
   366
    g->terminateHandler();
sl@0
   367
  }
sl@0
   368
  // If hook fn returns:
sl@0
   369
  abort();
sl@0
   370
}
sl@0
   371
sl@0
   372
sl@0
   373
#endif /* arm_exceptions_terminate_c */
sl@0
   374
#ifdef arm_exceptions_setterminate_c
sl@0
   375
sl@0
   376
terminate_handler std::set_terminate(terminate_handler h) throw()
sl@0
   377
{
sl@0
   378
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   379
  terminate_handler old = g->terminateHandler;
sl@0
   380
  g->terminateHandler = h;
sl@0
   381
  return old;
sl@0
   382
}
sl@0
   383
sl@0
   384
sl@0
   385
#endif /* arm_exceptions_setterminate_c */
sl@0
   386
#ifdef arm_exceptions_unexpected_c
sl@0
   387
sl@0
   388
/* ---- unexpected() etc ---- */
sl@0
   389
/* Comments as per terminate() */
sl@0
   390
sl@0
   391
void NAMES::default_unexpected_handler(void) {
sl@0
   392
  terminate();
sl@0
   393
}
sl@0
   394
sl@0
   395
#pragma exceptions_unwind
sl@0
   396
sl@0
   397
void std::unexpected(void)
sl@0
   398
{
sl@0
   399
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   400
sl@0
   401
  if (g->call_hook != NULL) {
sl@0
   402
    // Clear then call hook fn we were passed
sl@0
   403
    handler call_hook = g->call_hook;
sl@0
   404
    g->call_hook = NULL;
sl@0
   405
    call_hook();
sl@0
   406
  } else {
sl@0
   407
    // Call global hook fn
sl@0
   408
    g->unexpectedHandler();
sl@0
   409
  }
sl@0
   410
sl@0
   411
  // If hook fn returns:
sl@0
   412
  abort();
sl@0
   413
}
sl@0
   414
sl@0
   415
sl@0
   416
#endif /* arm_exceptions_unexpected_c */
sl@0
   417
#ifdef arm_exceptions_setunexpected_c
sl@0
   418
sl@0
   419
unexpected_handler std::set_unexpected(unexpected_handler h) throw()
sl@0
   420
{
sl@0
   421
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   422
  unexpected_handler old = g->unexpectedHandler;
sl@0
   423
  g->unexpectedHandler = h;
sl@0
   424
  return old;
sl@0
   425
}
sl@0
   426
sl@0
   427
sl@0
   428
#endif /* arm_exceptions_setunexpected_c */
sl@0
   429
#ifdef arm_exceptions_support_c
sl@0
   430
sl@0
   431
/* ---------- Helper functions: ---------- */
sl@0
   432
sl@0
   433
/* Two routines to determine whether two exceptions objects share a layout.
sl@0
   434
 * This is determined by checking whether the UCB exception_class members
sl@0
   435
 * are identical.
sl@0
   436
 * In principle we could use memcmp to perform this check (the code is
sl@0
   437
 * given below) but the check is quite frequent and so that is costly.
sl@0
   438
 * Therefore for efficiency we make use of the fact that the UCB is
sl@0
   439
 * word aligned, that the exception_class member is consequently
sl@0
   440
 * word aligned within it, and that we know the size of the member.
sl@0
   441
 * We take care elsewhere to only ever call the routines with pointers
sl@0
   442
 * to word-aligned addresses.
sl@0
   443
 */
sl@0
   444
sl@0
   445
#if 0
sl@0
   446
sl@0
   447
// Straightforward versions
sl@0
   448
sl@0
   449
bool NAMES::same_exceptions_class(const void *ec1, const void *ec2)
sl@0
   450
{
sl@0
   451
  return memcmp(ec1, ec2, EXCEPTIONS_CLASS_SIZE) == 0; // identical
sl@0
   452
}
sl@0
   453
sl@0
   454
// One of our exception objects, or not?
sl@0
   455
sl@0
   456
bool NAMES::is_foreign_exception(UCB *ucbp)
sl@0
   457
{
sl@0
   458
  return !NAMES::same_exceptions_class(&ucbp->exception_class, ARMCPP_EXCEPTIONS_CLASS);
sl@0
   459
}
sl@0
   460
sl@0
   461
#else
sl@0
   462
sl@0
   463
// Faster versions
sl@0
   464
sl@0
   465
bool NAMES::same_exceptions_class(const void *ec1, const void *ec2)
sl@0
   466
{
sl@0
   467
  uint32_t *ip1 = (uint32_t *)ec1;
sl@0
   468
  uint32_t *ip2 = (uint32_t *)ec2;
sl@0
   469
  return ip1[0] == ip2[0] && ip1[1] == ip2[1];
sl@0
   470
}
sl@0
   471
sl@0
   472
// One of our exception objects, or not?
sl@0
   473
sl@0
   474
bool NAMES::is_foreign_exception(UCB *ucbp)
sl@0
   475
{
sl@0
   476
  // Need a word-aligned copy of the string
sl@0
   477
  static const union {
sl@0
   478
    const char s[EXCEPTIONS_CLASS_SIZE+1]; int dummy;
sl@0
   479
  } is_foreign_exception_static = {ARMCPP_EXCEPTIONS_CLASS};
sl@0
   480
  return !NAMES::same_exceptions_class(&ucbp->exception_class, &is_foreign_exception_static.s);
sl@0
   481
}
sl@0
   482
sl@0
   483
#endif
sl@0
   484
sl@0
   485
sl@0
   486
#endif /* arm_exceptions_support_c */
sl@0
   487
#ifdef arm_exceptions_callterm_c
sl@0
   488
sl@0
   489
/* When the implementation wants to call terminate(), do the following:
sl@0
   490
 * Mark the object as "caught" so it can be rethrown.
sl@0
   491
 * Set the hook function for terminate() to call;
sl@0
   492
 * Mark the fact that terminate() has been called by the implementation;
sl@0
   493
 * We have to be careful - the implementation might encounter an error while
sl@0
   494
 * unwinding a foreign exception, and also it is possible this might be
sl@0
   495
 * called after failing to obtain a ucb.
sl@0
   496
 */
sl@0
   497
sl@0
   498
void NAMES::call_terminate_handler(UCB *ucbp)
sl@0
   499
{
sl@0
   500
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   501
sl@0
   502
  if (ucbp == NULL) {
sl@0
   503
    // Call global hook
sl@0
   504
    g->call_hook = g->terminateHandler;
sl@0
   505
  } else {
sl@0
   506
    // Extract the hook to call
sl@0
   507
    if (NAMES::is_foreign_exception(ucbp)) {
sl@0
   508
      // Someone else's
sl@0
   509
      g->call_hook = g->terminateHandler;  // best we can do under the circumstances
sl@0
   510
    } else {
sl@0
   511
      // One of ours
sl@0
   512
      __cxa_exception *ep = ucbp_to_ep(ucbp);
sl@0
   513
      g->call_hook = ep->terminateHandler; // the one in force at the point of throw
sl@0
   514
    }
sl@0
   515
  }
sl@0
   516
sl@0
   517
  g->implementation_ever_called_terminate = true;
sl@0
   518
  terminate();
sl@0
   519
  // never returns
sl@0
   520
}
sl@0
   521
sl@0
   522
sl@0
   523
void __cxa_call_terminate(UCB *ucbp)
sl@0
   524
{
sl@0
   525
  if (ucbp != NULL) // Record entry to (implicit) handler
sl@0
   526
    __cxa_begin_catch(ucbp);
sl@0
   527
sl@0
   528
  NAMES::call_terminate_handler(ucbp);
sl@0
   529
  // never returns
sl@0
   530
}
sl@0
   531
sl@0
   532
sl@0
   533
#endif /* arm_exceptions_callterm_c */
sl@0
   534
#ifdef arm_exceptions_callunex_c
sl@0
   535
sl@0
   536
/* When the implementation wants to call unexpected(), do the following:
sl@0
   537
 * Mark the object as "caught" so it can be rethrown.
sl@0
   538
 * Set the hook function for unexpected() to call;
sl@0
   539
 * Call unexpected and trap any throw to make sure it is acceptable.
sl@0
   540
 * We have to be careful - the implementation might encounter an error while
sl@0
   541
 * unwinding a foreign exception.
sl@0
   542
 */
sl@0
   543
sl@0
   544
#pragma exceptions_unwind
sl@0
   545
sl@0
   546
void __cxa_call_unexpected(UCB *ucbp)
sl@0
   547
{
sl@0
   548
sl@0
   549
  // Extract data we will need from the barrier cache before
sl@0
   550
  // anyone has a chance to overwrite it
sl@0
   551
sl@0
   552
  uint32_t rtti_count = ucbp->barrier_cache.bitpattern[1];
sl@0
   553
  uint32_t base = ucbp->barrier_cache.bitpattern[2];
sl@0
   554
  uint32_t stride = ucbp->barrier_cache.bitpattern[3];
sl@0
   555
  uint32_t rtti_offset_array_addr = ucbp->barrier_cache.bitpattern[4];
sl@0
   556
sl@0
   557
  // Also get the globals here and the eop
sl@0
   558
sl@0
   559
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   560
  __cxa_exception *ep = ucbp_to_ep(ucbp);
sl@0
   561
sl@0
   562
#ifdef ARM_EXCEPTIONS_ENABLED
sl@0
   563
  try {
sl@0
   564
#endif
sl@0
   565
sl@0
   566
    // Record entry to (implicit) handler
sl@0
   567
sl@0
   568
    __cxa_begin_catch(ucbp);
sl@0
   569
sl@0
   570
    // Now extract the hook to call
sl@0
   571
sl@0
   572
    if (NAMES::is_foreign_exception(ucbp)) {
sl@0
   573
      // Someone else's
sl@0
   574
      g->call_hook = g->unexpectedHandler;  // best we can do under the circumstances
sl@0
   575
    } else {
sl@0
   576
      // One of ours
sl@0
   577
      g->call_hook = ep->unexpectedHandler; // the one in force at the point of throw
sl@0
   578
    }
sl@0
   579
    unexpected();  // never returns normally, but might throw something
sl@0
   580
sl@0
   581
#ifdef ARM_EXCEPTIONS_ENABLED
sl@0
   582
  } catch (...) {
sl@0
   583
sl@0
   584
    // Unexpected() threw. This requires some delicacy.
sl@0
   585
    // There are 2 possibilities:
sl@0
   586
    // i) rethrow of the same object
sl@0
   587
    // ii) throw of a new object
sl@0
   588
    // Unexpected() is an implicit handler, and we manually called
sl@0
   589
    // __cxa_begin_catch on the ingoing object. We need to call
sl@0
   590
    // __cxa_end_catch on that object and, if the object is no longer
sl@0
   591
    // being handled (possible in case ii), this will cause its destruction.
sl@0
   592
    // The wrinkle is that in case ii the object is not on top of the catch
sl@0
   593
    // stack because we just caught something else.
sl@0
   594
sl@0
   595
    // Get hold of what was thrown (which we just caught).
sl@0
   596
sl@0
   597
    __cxa_exception *epnew = g->caughtExceptions;
sl@0
   598
sl@0
   599
    // Call __cxa_end_catch on the original object, taking care with the catch chain
sl@0
   600
sl@0
   601
    if (epnew == ep) {
sl@0
   602
      // rethrow - easy & safe - object is at top of chain and handlercount > 1
sl@0
   603
      __cxa_end_catch();
sl@0
   604
    } else {
sl@0
   605
      // not rethrow - unchain the top (new) object, clean up the next one,
sl@0
   606
      // and put the top object back
sl@0
   607
sl@0
   608
      // unchain
sl@0
   609
      g->caughtExceptions = epnew->nextCaughtException;
sl@0
   610
      // assert g->caughtExceptions == ep now
sl@0
   611
      // Decrement its handlercount (this might call a dtor if the count goes to 0,
sl@0
   612
      // and the dtor might throw - if it does, just give up)
sl@0
   613
      try {
sl@0
   614
	__cxa_end_catch();
sl@0
   615
      } catch(...) {
sl@0
   616
	terminate();
sl@0
   617
      }
sl@0
   618
      // Chain back in
sl@0
   619
      epnew->nextCaughtException = g->caughtExceptions;
sl@0
   620
      g->caughtExceptions = epnew;
sl@0
   621
    }
sl@0
   622
sl@0
   623
    // See whether what was thrown is permitted, and in passing
sl@0
   624
    // see if std::bad_exception is permitted
sl@0
   625
sl@0
   626
    bool bad_exception_permitted = false;
sl@0
   627
    uint32_t i;
sl@0
   628
    for (i = 0; i < rtti_count; i++) {
sl@0
   629
      void *matched_object;
sl@0
   630
      const type_info *fnspec = (const type_info *)(*(uint32_t *)rtti_offset_array_addr + base);
sl@0
   631
      if (__cxa_type_match(&(epnew->ucb), fnspec, &matched_object)) {
sl@0
   632
#ifdef PRINTED_DIAGNOSTICS
sl@0
   633
	printf("__cxa_call_unexpected: fnspec matched\n");
sl@0
   634
#endif
sl@0
   635
	throw; // got a match - propagate it
sl@0
   636
      }
sl@0
   637
      if (&typeid(std::bad_exception) == fnspec)
sl@0
   638
	bad_exception_permitted = true;
sl@0
   639
      rtti_offset_array_addr += stride;
sl@0
   640
    }
sl@0
   641
sl@0
   642
    // There was no match...
sl@0
   643
    if (bad_exception_permitted) throw std::bad_exception(); // transmute
sl@0
   644
sl@0
   645
    // Otherwise call epnew's terminate handler
sl@0
   646
    NAMES::call_terminate_handler(&epnew->ucb);
sl@0
   647
  }
sl@0
   648
#endif
sl@0
   649
}
sl@0
   650
sl@0
   651
sl@0
   652
#endif /* arm_exceptions_callunex_c */
sl@0
   653
#ifdef arm_exceptions_currenttype_c
sl@0
   654
sl@0
   655
/* Yield the type of the currently handled exception, or null if none or the
sl@0
   656
 * object is foreign.
sl@0
   657
 */
sl@0
   658
sl@0
   659
const type_info *__cxa_current_exception_type(void)
sl@0
   660
{
sl@0
   661
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   662
  __cxa_exception *ep = g->caughtExceptions;
sl@0
   663
  if (ep == NULL || NAMES::is_foreign_exception(&ep->ucb)) return NULL;
sl@0
   664
  return ep->exceptionType;
sl@0
   665
}
sl@0
   666
sl@0
   667
sl@0
   668
#endif /* arm_exceptions_currenttype_c */
sl@0
   669
#ifdef arm_exceptions_alloc_c
sl@0
   670
sl@0
   671
/* Allocate store for controlling an exception propagation */
sl@0
   672
sl@0
   673
void *__cxa_allocate_exception(size_t size)
sl@0
   674
{
sl@0
   675
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   676
sl@0
   677
  // Allocate store for a __cxa_exception header and the EO.
sl@0
   678
  // Allocated store should be thread-safe and persistent, and must do
sl@0
   679
  // something sensible if the allocation fails
sl@0
   680
sl@0
   681
  size_t total_size = size + sizeof(__cxa_exception);
sl@0
   682
  __cxa_exception *ep = (__cxa_exception *)malloc(total_size);
sl@0
   683
  if (ep == NULL) {
sl@0
   684
    // Try the emergency memory pool
sl@0
   685
    ep = (__cxa_exception *)__ARM_exceptions_buffer_allocate(g->emergency_buffer, total_size);
sl@0
   686
    if (ep == NULL) NAMES::call_terminate_handler(NULL);
sl@0
   687
  }
sl@0
   688
sl@0
   689
  UCB *ucbp = &ep->ucb;
sl@0
   690
sl@0
   691
  // Initialise the UCB
sl@0
   692
sl@0
   693
  memcpy(ucbp->exception_class, ARMCPP_EXCEPTIONS_CLASS, EXCEPTIONS_CLASS_SIZE);
sl@0
   694
  ucbp->exception_cleanup = NULL; /* initialise properly before throwing */
sl@0
   695
  ucbp->unwinder_cache.reserved1 = 0; /* required to do this */
sl@0
   696
sl@0
   697
  // Initialise parts of the LEO, in case copy-construction of the EO results
sl@0
   698
  // in a need to call terminate (via __cxa_call_terminate)
sl@0
   699
sl@0
   700
  ep->handlerCount = 0;                         // Not in any handlers
sl@0
   701
  ep->nextCaughtException = NULL;               // Not in any handlers
sl@0
   702
  ep->nextPropagatingException = NULL;          // Not saved over cleanup
sl@0
   703
  ep->propagationCount = 0;                     // Not propagating
sl@0
   704
  ep->terminateHandler = g->terminateHandler;   // Cache current terminate handler
sl@0
   705
  ep->unexpectedHandler = g->unexpectedHandler; // Cache current unexpected handler
sl@0
   706
sl@0
   707
  // Return pointer to the EO
sl@0
   708
sl@0
   709
  return ep + 1;
sl@0
   710
}
sl@0
   711
sl@0
   712
sl@0
   713
#endif /* arm_exceptions_alloc_c */
sl@0
   714
#ifdef arm_exceptions_free_c
sl@0
   715
sl@0
   716
/* Free store allocated by __cxa_allocate_exception */
sl@0
   717
sl@0
   718
void __cxa_free_exception(void *eop)
sl@0
   719
{
sl@0
   720
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   721
  char *ep = (char *)eop - sizeof(__cxa_exception);
sl@0
   722
  if (__ARM_exceptions_buffer_free(g->emergency_buffer, ep)) return;
sl@0
   723
  free(ep);
sl@0
   724
}
sl@0
   725
sl@0
   726
sl@0
   727
#endif /* arm_exceptions_free_c */
sl@0
   728
#ifdef arm_exceptions_throw_c
sl@0
   729
sl@0
   730
/* This routine is called when a foreign runtime catches one of our exception
sl@0
   731
 * objects and then exits its catch by a means other than rethrow.
sl@0
   732
 * We should clean it up as if we had caught it ourselves.
sl@0
   733
 */
sl@0
   734
sl@0
   735
static void external_exception_termination(_Unwind_Reason_Code c, UCB *ucbp)
sl@0
   736
{
sl@0
   737
  NAMES::eh_catch_semantics(ucbp);
sl@0
   738
  __cxa_end_catch();
sl@0
   739
}
sl@0
   740
sl@0
   741
sl@0
   742
/* Initiate a throw */
sl@0
   743
sl@0
   744
#pragma push
sl@0
   745
#pragma exceptions_unwind
sl@0
   746
sl@0
   747
void __cxa_throw(void *eop, const type_info *t, cppdtorptr d)
sl@0
   748
{
sl@0
   749
  __cxa_exception *ep = (__cxa_exception *)((char *)eop - sizeof(__cxa_exception));
sl@0
   750
  UCB *ucbp = &ep->ucb;
sl@0
   751
sl@0
   752
  // Initialise the remaining LEO and UCB fields not done by __cxa_allocate_exception
sl@0
   753
sl@0
   754
  ucbp->exception_cleanup = external_exception_termination;
sl@0
   755
  ep->exceptionType = t;
sl@0
   756
  ep->exceptionDestructor = d;
sl@0
   757
  ep->propagationCount = 1;      // Propagating by 1 throw
sl@0
   758
sl@0
   759
  // Increment the uncaught C++ exceptions count
sl@0
   760
sl@0
   761
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   762
  g->uncaughtExceptions++;
sl@0
   763
sl@0
   764
  // Tell debugger what's happening
sl@0
   765
sl@0
   766
  DEBUGGER_BOTTLENECK(ucbp, _UASUBSYS_CPP, _UAACT_STARTING, t);
sl@0
   767
sl@0
   768
  // Initiate unwinding - if we get control back, call C++ routine terminate()
sl@0
   769
sl@0
   770
  _Unwind_RaiseException(ucbp);
sl@0
   771
sl@0
   772
#ifdef PRINTED_DIAGNOSTICS
sl@0
   773
  printf("__cxa_throw: throw failed\n");
sl@0
   774
#endif
sl@0
   775
sl@0
   776
  __cxa_call_terminate(ucbp);
sl@0
   777
}
sl@0
   778
sl@0
   779
#pragma pop
sl@0
   780
sl@0
   781
/* ----- Type matching: ----- */
sl@0
   782
sl@0
   783
/* This is located here so that (in ARM's implementation) it is only retained in
sl@0
   784
 * an image if the application itself throws.
sl@0
   785
 */
sl@0
   786
sl@0
   787
/* Type matching functions.
sl@0
   788
 * C++ DR126 says the matching rules for fnspecs are intended to be the same as
sl@0
   789
 * those for catch:
sl@0
   790
 * "A function is said to allow an exception of type E if its exception-specification
sl@0
   791
 * contains a type T for which a handler of type T would be a match (15.3 except.handle)
sl@0
   792
 * for an exception of type E."
sl@0
   793
 * Thus we have a single type matching rule.
sl@0
   794
 */
sl@0
   795
sl@0
   796
/* Helper macros: */
sl@0
   797
sl@0
   798
#define CV_quals_of_pointee(P) (((const abi::__pbase_type_info *)(P))->__flags & \
sl@0
   799
		                (abi::__pbase_type_info::__const_mask | \
sl@0
   800
		                 abi::__pbase_type_info::__volatile_mask))
sl@0
   801
sl@0
   802
#define is_const(QUALS) (((QUALS) & abi::__pbase_type_info::__const_mask) != 0)
sl@0
   803
sl@0
   804
#define any_qualifier_missing(TEST_QUALS, REF_QUALS) ((~(TEST_QUALS) & (REF_QUALS)) != 0)
sl@0
   805
sl@0
   806
/* A routine is required for derived class to base class conversion.
sl@0
   807
 * This is obtained via a macro definition DERIVED_TO_BASE_CONVERSION
sl@0
   808
 * in unwind_env.h.
sl@0
   809
 */
sl@0
   810
sl@0
   811
/* External entry point:
sl@0
   812
 * Type check the c++ rtti object for compatibility against the type of
sl@0
   813
 * the object containing the ucb. Return a pointer to the matched object
sl@0
   814
 * (possibly a non-leftmost baseclass of the exception object)
sl@0
   815
 */
sl@0
   816
bool __cxa_type_match(UCB *ucbp, const type_info *match_type, void **matched_objectpp)
sl@0
   817
{
sl@0
   818
  if (NAMES::is_foreign_exception(ucbp))
sl@0
   819
    return false;
sl@0
   820
sl@0
   821
  __cxa_exception *ep = ucbp_to_ep(ucbp);
sl@0
   822
  const type_info *throw_type = ep->exceptionType;
sl@0
   823
  bool previous_qualifiers_include_const = true; // for pointer qualification conversion
sl@0
   824
  unsigned int pointer_depth = 0;
sl@0
   825
  void *original_objectp = ep + 1;
sl@0
   826
  void *current_objectp = original_objectp;
sl@0
   827
sl@0
   828
  for (;;) {
sl@0
   829
sl@0
   830
    // Match if identical
sl@0
   831
sl@0
   832
    if (throw_type == match_type) {
sl@0
   833
      *matched_objectpp = original_objectp;
sl@0
   834
#ifdef PRINTED_DIAGNOSTICS
sl@0
   835
      printf("__cxa_type_match: success (exact match after any ptrs)\n");
sl@0
   836
#endif
sl@0
   837
      return true;
sl@0
   838
    }
sl@0
   839
sl@0
   840
    // Fail if one is a pointer and the other isn't
sl@0
   841
sl@0
   842
    const type_info *type_throw_type = &typeid(*throw_type);
sl@0
   843
    const type_info *type_match_type = &typeid(*match_type);
sl@0
   844
sl@0
   845
    if ((type_throw_type == &typeid(abi::__pointer_type_info) ||
sl@0
   846
	 type_match_type == &typeid(abi::__pointer_type_info)) &&
sl@0
   847
	type_throw_type != type_match_type) {
sl@0
   848
#ifdef PRINTED_DIAGNOSTICS
sl@0
   849
      printf("__cxa_type_match: failed (mixed ptr/non-ptr)\n");
sl@0
   850
#endif
sl@0
   851
      return false;
sl@0
   852
    }
sl@0
   853
sl@0
   854
    // Both are pointers or neither is
sl@0
   855
    if (type_throw_type == &typeid(abi::__pointer_type_info)) {
sl@0
   856
      // Both are pointers
sl@0
   857
#ifdef PRINTED_DIAGNOSTICS
sl@0
   858
      printf("__cxa_type_match: throwing a ptr\n");
sl@0
   859
#endif
sl@0
   860
      pointer_depth++;
sl@0
   861
      // Check match_type is at least as CV-qualified as throw_type
sl@0
   862
      unsigned int match_quals = CV_quals_of_pointee(match_type);
sl@0
   863
      unsigned int throw_quals = CV_quals_of_pointee(throw_type);
sl@0
   864
      if (any_qualifier_missing(match_quals, throw_quals)) {
sl@0
   865
#ifdef PRINTED_DIAGNOSTICS
sl@0
   866
	printf("__cxa_type_match: failed (missing qualifiers)\n");
sl@0
   867
#endif
sl@0
   868
	return false;
sl@0
   869
      }
sl@0
   870
      // If the match type has additional qualifiers not found in the
sl@0
   871
      // throw type, any previous qualifiers must have included const
sl@0
   872
      if (any_qualifier_missing(throw_quals, match_quals) &&
sl@0
   873
	  !previous_qualifiers_include_const) {
sl@0
   874
#ifdef PRINTED_DIAGNOSTICS
sl@0
   875
	printf("__cxa_type_match: failed (not all qualifiers have const)\n");
sl@0
   876
#endif
sl@0
   877
	return false;
sl@0
   878
      }
sl@0
   879
      if (!is_const(match_quals))
sl@0
   880
	previous_qualifiers_include_const = false;
sl@0
   881
      throw_type = ((const abi::__pbase_type_info *)throw_type)->__pointee;
sl@0
   882
      match_type = ((const abi::__pbase_type_info *)match_type)->__pointee;
sl@0
   883
      if (current_objectp != NULL)
sl@0
   884
        current_objectp = *(void **)current_objectp;
sl@0
   885
      continue;
sl@0
   886
    }
sl@0
   887
sl@0
   888
    // Neither is a pointer now but qualification conversion has been done.
sl@0
   889
    // See if pointer conversion on the original was possible.
sl@0
   890
    // T* will match void*
sl@0
   891
sl@0
   892
    if (pointer_depth == 1 && match_type == &typeid(void)) {
sl@0
   893
      *matched_objectpp = original_objectp;
sl@0
   894
#ifdef PRINTED_DIAGNOSTICS
sl@0
   895
      printf("__cxa_type_match: success(conversion to void *)\n");
sl@0
   896
#endif
sl@0
   897
      return true;
sl@0
   898
    }
sl@0
   899
sl@0
   900
    // Else if we have 2 class types, a derived class is matched by a
sl@0
   901
    // non-ambiguous public base class (perhaps not a leftmost one).
sl@0
   902
    // __si_class_type_info and __vmi_class_type_info are classes with bases.
sl@0
   903
sl@0
   904
    void *matched_base_p;
sl@0
   905
sl@0
   906
    if (pointer_depth < 2 &&
sl@0
   907
	(type_throw_type == &typeid(abi::__si_class_type_info) ||
sl@0
   908
	 type_throw_type == &typeid(abi::__vmi_class_type_info))) {
sl@0
   909
      if (DERIVED_TO_BASE_CONVERSION(current_objectp, &matched_base_p,
sl@0
   910
				     throw_type, match_type)) {
sl@0
   911
#ifdef PRINTED_DIAGNOSTICS
sl@0
   912
	printf("__cxa_type_match: success (matched base 0x%x of 0x%x%s, thrown object 0x%x)\n",
sl@0
   913
	       matched_base_p, current_objectp,
sl@0
   914
	       pointer_depth == 0 ? "" : " via ptr",
sl@0
   915
	       original_objectp);
sl@0
   916
#endif
sl@0
   917
	*matched_objectpp = pointer_depth == 0 ? matched_base_p : original_objectp;
sl@0
   918
	return true;
sl@0
   919
      } else {
sl@0
   920
#ifdef PRINTED_DIAGNOSTICS
sl@0
   921
	printf("__cxa_type_match: failed (derived to base failed)\n");
sl@0
   922
#endif
sl@0
   923
	return false;
sl@0
   924
      }
sl@0
   925
    }
sl@0
   926
sl@0
   927
#ifdef PRINTED_DIAGNOSTICS
sl@0
   928
    printf("__cxa_type_match: failed (types simply differ)\n");
sl@0
   929
#endif
sl@0
   930
    return false;
sl@0
   931
  } /* for */
sl@0
   932
}
sl@0
   933
sl@0
   934
sl@0
   935
/* For debugging purposes: */
sl@0
   936
#ifdef DEBUG
sl@0
   937
extern "C" bool debug__cxa_type_match(void *objptr,
sl@0
   938
				      const type_info *throw_type,
sl@0
   939
				      const type_info *catch_type,
sl@0
   940
				      void **matched_objectpp)
sl@0
   941
{
sl@0
   942
  /* Create enough of an exception object that the type-matcher can run, then
sl@0
   943
   * check the type. Objptr is expected to be the result of a call to
sl@0
   944
   * __cxa_allocate_exception, which has then been copy-constructed.
sl@0
   945
   */
sl@0
   946
  __cxa_exception *e = ((__cxa_exception *)objptr) - 1;
sl@0
   947
  e->exceptionType = throw_type;
sl@0
   948
  return __cxa_type_match(&e->ucb, catch_type, matched_objectpp);
sl@0
   949
}
sl@0
   950
#endif
sl@0
   951
sl@0
   952
sl@0
   953
#endif /* arm_exceptions_throw_c */
sl@0
   954
#ifdef arm_exceptions_rethrow_c
sl@0
   955
sl@0
   956
/* Redeclare _Unwind_RaiseException as weak (if WEAKDECL is defined
sl@0
   957
 * appropriately) so the use from __cxa_rethrow does not on its own
sl@0
   958
 * force the unwind library to be loaded.
sl@0
   959
 */
sl@0
   960
sl@0
   961
extern "C" WEAKDECL _Unwind_Reason_Code _Unwind_RaiseException(UCB *ucbp);
sl@0
   962
sl@0
   963
#pragma exceptions_unwind
sl@0
   964
sl@0
   965
void __cxa_rethrow(void)
sl@0
   966
{
sl@0
   967
  // Recover the exception object - it is the most recent caught exception object
sl@0
   968
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
   969
  __cxa_exception *ep = g->caughtExceptions;
sl@0
   970
  bool foreign;
sl@0
   971
sl@0
   972
  // Must call terminate here if no such exception
sl@0
   973
  if (ep == NULL) NAMES::call_terminate_handler(NULL);
sl@0
   974
sl@0
   975
  UCB *ucbp = &ep->ucb;
sl@0
   976
sl@0
   977
  // Mark the object as being propagated by throw, preventing multiple
sl@0
   978
  // propagation and also permitting __cxa_end_catch to do the right
sl@0
   979
  // thing when it is called from the handler's cleanup.
sl@0
   980
sl@0
   981
  ep->propagationCount++;
sl@0
   982
sl@0
   983
  // Now reraise, taking care with foreign exceptions
sl@0
   984
sl@0
   985
  foreign = NAMES::is_foreign_exception(ucbp);
sl@0
   986
  if (foreign) {
sl@0
   987
    // Indirect through the intermediate object to the foreign ucb
sl@0
   988
    ucbp = (UCB *)ep->exceptionType;
sl@0
   989
  } else {
sl@0
   990
    // Increment the uncaught C++ exceptions count
sl@0
   991
    g->uncaughtExceptions++;
sl@0
   992
  }
sl@0
   993
sl@0
   994
  // Tell debugger what's happening
sl@0
   995
sl@0
   996
  DEBUGGER_BOTTLENECK(ucbp, _UASUBSYS_CPP, _UAACT_STARTING, foreign ? NULL : ep->exceptionType);
sl@0
   997
sl@0
   998
  // Initiate unwinding - if we get control back, call C++ routine terminate()
sl@0
   999
sl@0
  1000
  _Unwind_RaiseException(ucbp);
sl@0
  1001
sl@0
  1002
#ifdef PRINTED_DIAGNOSTICS
sl@0
  1003
  printf("__cxa_rethrow: throw failed\n");
sl@0
  1004
#endif
sl@0
  1005
sl@0
  1006
  __cxa_call_terminate(ucbp);
sl@0
  1007
}
sl@0
  1008
sl@0
  1009
#endif /* arm_exceptions_rethrow_c */
sl@0
  1010
#ifdef arm_exceptions_foreign_c
sl@0
  1011
sl@0
  1012
/* During catch and cleanup, foreign exception objects are dealt with using
sl@0
  1013
 * an intermediate __cxa_exception block in the appropriate exceptions
sl@0
  1014
 * chain. This block has the same exception_class as the real foreign
sl@0
  1015
 * ucb, and points to the real ucb via the intermediate block's exceptionType
sl@0
  1016
 * field. This helper function checks whether it has been passed such an
sl@0
  1017
 * intermediate block and sets one up if not. Only call it when the UCB
sl@0
  1018
 * is known to belong to a foreign exception.
sl@0
  1019
 */
sl@0
  1020
sl@0
  1021
__cxa_exception *NAMES::get_foreign_intermediary(__cxa_exception *head_ep, UCB *ucbp)
sl@0
  1022
{
sl@0
  1023
  if (head_ep != NULL) {
sl@0
  1024
    UCB *head_ucbp = &head_ep->ucb;
sl@0
  1025
    if (NAMES::same_exceptions_class(&head_ucbp->exception_class, &ucbp->exception_class) &&
sl@0
  1026
	(UCB *)head_ep->exceptionType == ucbp)
sl@0
  1027
      return head_ep;
sl@0
  1028
  }
sl@0
  1029
sl@0
  1030
  // Create an intermediate block. Only initialise as much as necessary
sl@0
  1031
  __cxa_exception *ep = ((__cxa_exception *)__cxa_allocate_exception(0)) - 1;
sl@0
  1032
  UCB *new_ucbp = &ep->ucb;
sl@0
  1033
  memcpy(new_ucbp->exception_class, ucbp->exception_class, EXCEPTIONS_CLASS_SIZE);
sl@0
  1034
  ep->propagationCount = 0;                     // Not propagating
sl@0
  1035
  ep->handlerCount = 0;                         // Not handled
sl@0
  1036
  ep->nextCaughtException = NULL;               // Not in chain
sl@0
  1037
  ep->exceptionType = (const type_info *)ucbp;  // The foreign UCB
sl@0
  1038
  return ep;
sl@0
  1039
}
sl@0
  1040
sl@0
  1041
sl@0
  1042
#endif /* arm_exceptions_foreign_c */
sl@0
  1043
#ifdef arm_exceptions_cleanup_c
sl@0
  1044
sl@0
  1045
bool __cxa_begin_cleanup(UCB *ucbp)
sl@0
  1046
{
sl@0
  1047
  // Indicate that a cleanup is about to start.
sl@0
  1048
  // Save the exception pointer over the cleanup for recovery later, using a chain.
sl@0
  1049
  // If we allowed the exception to be rethrown in a cleanup, then
sl@0
  1050
  // the object might appear multiple times at the head of this chain,
sl@0
  1051
  // and the propagationCount could be used to track this - at this point,
sl@0
  1052
  // the object is logically in the chain propagationCount-1 times, and
sl@0
  1053
  // physically 0 or 1 times. Thus if propagationCount == 1 we should insert
sl@0
  1054
  // it physically. A similar rule is used for physical removal in
sl@0
  1055
  //__cxa_end_cleanup.
sl@0
  1056
  // Foreign exceptions are handled via an intermediate __cxa_exception object
sl@0
  1057
  // in a similar way as __cxa_begin_catch.
sl@0
  1058
sl@0
  1059
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
  1060
  __cxa_exception *ep;
sl@0
  1061
sl@0
  1062
  if (NAMES::is_foreign_exception(ucbp)) {
sl@0
  1063
    ep = NAMES::get_foreign_intermediary(g->propagatingExceptions, ucbp);
sl@0
  1064
    ep->propagationCount++;  // Indicate one (or one additional) propagation
sl@0
  1065
  } else {
sl@0
  1066
    ep = ucbp_to_ep(ucbp);
sl@0
  1067
  }
sl@0
  1068
sl@0
  1069
  if (ep->propagationCount == 1) {
sl@0
  1070
    // Insert into chain
sl@0
  1071
    ep->nextPropagatingException = g->propagatingExceptions;
sl@0
  1072
    g->propagatingExceptions = ep;
sl@0
  1073
  }
sl@0
  1074
sl@0
  1075
  return true;
sl@0
  1076
}
sl@0
  1077
sl@0
  1078
sl@0
  1079
// Helper function for __cxa_end_cleanup
sl@0
  1080
sl@0
  1081
extern "C" UCB * __ARM_cxa_end_cleanup(void)
sl@0
  1082
{
sl@0
  1083
  // Recover and return the currently propagating exception (from the
sl@0
  1084
  // head of the propagatingExceptions chain).
sl@0
  1085
  // propagationCount at this moment is a logical count of how many times the
sl@0
  1086
  // item is in the chain so physically unchain it when this count is 1.
sl@0
  1087
  // Foreign exceptions use an intermediary.
sl@0
  1088
sl@0
  1089
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
  1090
  __cxa_exception *ep = g->propagatingExceptions;
sl@0
  1091
sl@0
  1092
  if (ep == NULL) terminate();
sl@0
  1093
sl@0
  1094
  UCB *ucbp = &ep->ucb;
sl@0
  1095
  if (NAMES::is_foreign_exception(ucbp)) {
sl@0
  1096
    // Get the foreign ucb
sl@0
  1097
    ucbp = (UCB *)ep->exceptionType;
sl@0
  1098
    if (ep->propagationCount == 1) {
sl@0
  1099
      // Free the intermediate ucb (see description in __cxa_begin_catch)
sl@0
  1100
      void *eop = (void *)(ep + 1);
sl@0
  1101
      g->propagatingExceptions = ep->nextPropagatingException;
sl@0
  1102
      __cxa_free_exception(eop);
sl@0
  1103
    } else {
sl@0
  1104
      ep->propagationCount--;
sl@0
  1105
    }
sl@0
  1106
  } else {
sl@0
  1107
    // Not foreign
sl@0
  1108
    if (ep->propagationCount == 1) { // logically in chain once - so unchain
sl@0
  1109
      g->propagatingExceptions = ep->nextPropagatingException;
sl@0
  1110
    }
sl@0
  1111
  }
sl@0
  1112
  return ucbp;
sl@0
  1113
}
sl@0
  1114
sl@0
  1115
// __cxa_end_cleanup is called at the end of a cleanup fragment.
sl@0
  1116
// It must do the C++ housekeeping, then call _Unwind_Resume, but it must
sl@0
  1117
// damage no significant registers in the process.
sl@0
  1118
sl@0
  1119
__asm void __cxa_end_cleanup(void) {
sl@0
  1120
  extern __ARM_cxa_end_cleanup;
sl@0
  1121
  extern _Unwind_Resume WEAKASMDECL;
sl@0
  1122
sl@0
  1123
#ifdef __thumb
sl@0
  1124
  preserve8;                   // This is preserve8 (ARM assembler heuristics are inadequate)
sl@0
  1125
  push {r1-r7};
sl@0
  1126
  mov r2, r8;
sl@0
  1127
  mov r3, r9;
sl@0
  1128
  mov r4, r10;
sl@0
  1129
  mov r5, r11;
sl@0
  1130
  push {r1-r5};
sl@0
  1131
  bl __ARM_cxa_end_cleanup;    // returns UCB address in r0
sl@0
  1132
  pop {r1-r5};
sl@0
  1133
  mov r8, r2;
sl@0
  1134
  mov r9, r3;
sl@0
  1135
  mov r10, r4;
sl@0
  1136
  mov r11, r5;
sl@0
  1137
  pop {r1-r7};
sl@0
  1138
  bl _Unwind_Resume;           // won't return
sl@0
  1139
#else
sl@0
  1140
  stmfd r13!, {r1-r12}
sl@0
  1141
  bl __ARM_cxa_end_cleanup;    // returns UCB address in r0
sl@0
  1142
  ldmia r13!, {r1-r12};
sl@0
  1143
  b _Unwind_Resume;            // won't return
sl@0
  1144
#endif
sl@0
  1145
}
sl@0
  1146
sl@0
  1147
sl@0
  1148
#endif /* arm_exceptions_cleanup_c */
sl@0
  1149
#ifdef arm_exceptions_catchsemantics_c
sl@0
  1150
sl@0
  1151
/* Update date structures as if catching an object.
sl@0
  1152
 * Call this from __cxa_begin_catch when actually catching an object,
sl@0
  1153
 * and from external_exception_termination when called by a foreign runtime
sl@0
  1154
 * after one of our objects was caught.
sl@0
  1155
 */
sl@0
  1156
sl@0
  1157
void NAMES::eh_catch_semantics(UCB *ucbp)
sl@0
  1158
{
sl@0
  1159
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
  1160
  __cxa_exception *ep;
sl@0
  1161
sl@0
  1162
  if (NAMES::is_foreign_exception(ucbp)) {
sl@0
  1163
    // Foreign exception. Get the associated intermediary block or
sl@0
  1164
    // make one if there isn't one already.
sl@0
  1165
    // In the case of a rethrow, the foreign object may already be on
sl@0
  1166
    // the handled exceptions chain (it will be first).
sl@0
  1167
    ep = NAMES::get_foreign_intermediary(g->caughtExceptions, ucbp);
sl@0
  1168
  } else {
sl@0
  1169
    // Not foreign
sl@0
  1170
    ep = ucbp_to_ep(ucbp);
sl@0
  1171
    // Decrement the propagation count
sl@0
  1172
    ep->propagationCount--;
sl@0
  1173
    // Decrement the total uncaught C++ exceptions count
sl@0
  1174
    g->uncaughtExceptions--;
sl@0
  1175
  }
sl@0
  1176
sl@0
  1177
  // Common code for our EO's, and foreign ones where we work on the intermediate EO
sl@0
  1178
sl@0
  1179
  // Increment the handler count for this exception object
sl@0
  1180
  ep->handlerCount++;
sl@0
  1181
sl@0
  1182
  // Push the ep onto the "handled exceptions" chain if it is not already there.
sl@0
  1183
  // (If catching a rethrow, it may already be there)
sl@0
  1184
sl@0
  1185
  if (ep->nextCaughtException == NULL) {
sl@0
  1186
    ep->nextCaughtException = g->caughtExceptions;
sl@0
  1187
    g->caughtExceptions = ep;
sl@0
  1188
  }
sl@0
  1189
}
sl@0
  1190
sl@0
  1191
sl@0
  1192
#endif /* arm_exceptions_catchsemantics_c */
sl@0
  1193
#ifdef arm_exceptions_begincatch_c
sl@0
  1194
sl@0
  1195
void *__cxa_begin_catch(UCB *ucbp)
sl@0
  1196
{
sl@0
  1197
  void *match = (void *)ucbp->barrier_cache.bitpattern[0]; // The matched object, if any
sl@0
  1198
sl@0
  1199
  // Update the data structures
sl@0
  1200
sl@0
  1201
  NAMES::eh_catch_semantics(ucbp);
sl@0
  1202
sl@0
  1203
  // Tell the unwinder the exception propagation has finished,
sl@0
  1204
  // and return the object pointer
sl@0
  1205
sl@0
  1206
  _Unwind_Complete(ucbp);
sl@0
  1207
  return match;
sl@0
  1208
}
sl@0
  1209
sl@0
  1210
sl@0
  1211
#endif /* arm_exceptions_begincatch_c */
sl@0
  1212
#ifdef arm_exceptions_endcatch_c
sl@0
  1213
sl@0
  1214
#pragma exceptions_unwind
sl@0
  1215
sl@0
  1216
void __cxa_end_catch(void)
sl@0
  1217
{
sl@0
  1218
  // Recover the exception object - it is the most recent caught exception object
sl@0
  1219
  __cxa_eh_globals *g = __cxa_get_globals();
sl@0
  1220
  __cxa_exception *ep = g->caughtExceptions;
sl@0
  1221
sl@0
  1222
  if (ep == NULL) terminate();
sl@0
  1223
sl@0
  1224
  // Rethrow in progress?
sl@0
  1225
sl@0
  1226
  bool object_being_rethrown = ep->propagationCount != 0;
sl@0
  1227
sl@0
  1228
  // Decrement the handler count for this exception object
sl@0
  1229
  ep->handlerCount--;
sl@0
  1230
sl@0
  1231
  // Unstack the object if it is no longer being handled anywhere.
sl@0
  1232
  // Destroy and free the object if it is no longer alive -
sl@0
  1233
  // it is dead if its handler count becomes 0, unless it is
sl@0
  1234
  // about to be rethrown.
sl@0
  1235
  // If the dtor throws, allow its exception to propagate.
sl@0
  1236
  // Do different things if it is a foreign exception object.
sl@0
  1237
sl@0
  1238
  if (ep->handlerCount == 0) {
sl@0
  1239
    void *eop = (void *)(ep + 1);
sl@0
  1240
    UCB *ucbp = &ep->ucb;
sl@0
  1241
    bool foreign = NAMES::is_foreign_exception(ucbp);
sl@0
  1242
sl@0
  1243
    // Unstack it from the caught exceptions stack - it is guaranteed to be top item.
sl@0
  1244
    g->caughtExceptions = ep->nextCaughtException;
sl@0
  1245
sl@0
  1246
    if (foreign) {
sl@0
  1247
      // Get the foreign ucb and free the intermediate ucb (see description in __cxa_begin_catch)
sl@0
  1248
      ucbp = (UCB *)ep->exceptionType;
sl@0
  1249
      __cxa_free_exception(eop);
sl@0
  1250
    } else {
sl@0
  1251
      ep->nextCaughtException = NULL;  // So __cxa_begin_catch knows it isn't in the chain
sl@0
  1252
    }
sl@0
  1253
sl@0
  1254
    // Now destroy the exception object if it's no longer needed
sl@0
  1255
    if (!object_being_rethrown) {
sl@0
  1256
      if (foreign) {
sl@0
  1257
sl@0
  1258
	// Notify the foreign language, if it so requested
sl@0
  1259
	if (ucbp->exception_cleanup != NULL)
sl@0
  1260
	  (ucbp->exception_cleanup)(_URC_FOREIGN_EXCEPTION_CAUGHT, ucbp);
sl@0
  1261
sl@0
  1262
      } else {
sl@0
  1263
sl@0
  1264
        // One of our objects: do C++-specific semantics
sl@0
  1265
sl@0
  1266
	if (ep->exceptionDestructor != NULL) {
sl@0
  1267
	  // Run the dtor. If it throws, free the memory anyway and
sl@0
  1268
	  // propagate the new exception.
sl@0
  1269
#ifdef ARM_EXCEPTIONS_ENABLED
sl@0
  1270
	  try {
sl@0
  1271
	    (ep->exceptionDestructor)(eop);
sl@0
  1272
	  } catch(...) {
sl@0
  1273
	    // Free the memory and reraise
sl@0
  1274
	    __cxa_free_exception(eop);
sl@0
  1275
	    throw;
sl@0
  1276
	  }
sl@0
  1277
#else
sl@0
  1278
	  (ep->exceptionDestructor)(eop);
sl@0
  1279
#endif
sl@0
  1280
	}
sl@0
  1281
	// Dtor (if there was one) didn't throw. Free the memory.
sl@0
  1282
	__cxa_free_exception(eop);
sl@0
  1283
      }  // !foreign
sl@0
  1284
    }  // !object_being_rethrown
sl@0
  1285
  }  // ep->handlerCount == 0
sl@0
  1286
}
sl@0
  1287
sl@0
  1288
sl@0
  1289
#endif /* arm_exceptions_endcatch_c */
sl@0
  1290
#ifdef arm_exceptions_bad_typeid_c
sl@0
  1291
sl@0
  1292
#pragma exceptions_unwind
sl@0
  1293
sl@0
  1294
void __cxa_bad_typeid(void)
sl@0
  1295
{
sl@0
  1296
  throw std::bad_typeid();
sl@0
  1297
}
sl@0
  1298
sl@0
  1299
sl@0
  1300
#endif /* arm_exceptions_bad_typeid_c */
sl@0
  1301
#ifdef arm_exceptions_bad_cast_c
sl@0
  1302
sl@0
  1303
#pragma exceptions_unwind
sl@0
  1304
sl@0
  1305
void __cxa_bad_cast(void)
sl@0
  1306
{
sl@0
  1307
  throw std::bad_cast();
sl@0
  1308
}
sl@0
  1309
sl@0
  1310
sl@0
  1311
#endif /* arm_exceptions_bad_cast_c */