source: libcfa/src/exception.c@ ae2c27a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since ae2c27a was 915aa11, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

start ARM stubs for exception handling

  • Property mode set to 100644
File size: 22.0 KB
RevLine 
[fa4805f]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// exception.c --
8//
9// Author : Andrew Beach
10// Created On : Mon Jun 26 15:13:00 2017
[67ca73e]11// Last Modified By : Peter A. Buhr
[915aa11]12// Last Modified On : Sat Aug 29 15:52:22 2020
13// Update Count : 34
[fa4805f]14//
15
[9640813]16// Normally we would get this from the CFA prelude.
[cbce272]17#include <stddef.h> // for size_t
18
[fa4805f]19#include "exception.h"
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <unwind.h>
[73abe95]24#include <bits/debug.hfa>
[80ec409]25#include "concurrency/invoke.h"
[a201f7b]26#include "stdhdr/assert.h"
[fa4805f]27
[915aa11]28#if defined( __ARM_ARCH )
29#warning FIX ME: temporary hack to keep ARM build working
[b947fb2]30#ifndef _URC_FATAL_PHASE1_ERROR
[73530d9]31#define _URC_FATAL_PHASE1_ERROR 3
[b947fb2]32#endif // ! _URC_FATAL_PHASE1_ERROR
33#ifndef _URC_FATAL_PHASE2_ERROR
34#define _URC_FATAL_PHASE2_ERROR 2
35#endif // ! _URC_FATAL_PHASE2_ERROR
[915aa11]36#endif // __ARM_ARCH
[b947fb2]37
[fa4805f]38#include "lsda.h"
39
[73530d9]40/* The exception class for our exceptions. Because of the vendor component
41 * its value would not be standard.
42 * Vendor: UWPL
43 * Language: CFA\0
44 */
45const _Unwind_Exception_Class __cfaehm_exception_class = 0x4c50575500414643;
[cbce272]46
47// Base exception vtable is abstract, you should not have base exceptions.
[3090127]48struct __cfaehm_base_exception_t_vtable
49 ___cfaehm_base_exception_t_vtable_instance = {
[cbce272]50 .parent = NULL,
51 .size = 0,
52 .copy = NULL,
53 .free = NULL,
54 .msg = NULL
55};
56
57
[86d5ba7c]58// Get the current exception context.
59// There can be a single global until multithreading occurs, then each stack
[5715d43]60// needs its own. We get this from libcfathreads (no weak attribute).
61__attribute__((weak)) struct exception_context_t * this_exception_context() {
62 static struct exception_context_t shared_stack = {NULL, NULL};
[86d5ba7c]63 return &shared_stack;
64}
[fa4805f]65
66
67// RESUMPTION ================================================================
68
[f1b6671]69static void reset_top_resume(struct __cfaehm_try_resume_node ** store) {
70 this_exception_context()->top_resume = *store;
71}
72
[046a890]73void __cfaehm_throw_resume(exception_t * except, void (*defaultHandler)(exception_t *)) {
[2a3b019]74 struct exception_context_t * context = this_exception_context();
[fa4805f]75
[851fd92]76 __cfadbg_print_safe(exception, "Throwing resumption exception\n");
[fa4805f]77
[381132b]78 {
79 __attribute__((cleanup(reset_top_resume)))
80 struct __cfaehm_try_resume_node * original_head = context->top_resume;
81 struct __cfaehm_try_resume_node * current = context->top_resume;
82
83 for ( ; current ; current = current->next) {
84 context->top_resume = current->next;
85 if (current->handler(except)) {
86 return;
87 }
[fa4805f]88 }
[381132b]89 } // End the search and return to the top of the stack.
[fa4805f]90
[046a890]91 // No handler found, fall back to the default operation.
[851fd92]92 __cfadbg_print_safe(exception, "Unhandled exception\n");
[046a890]93 defaultHandler(except);
[fa4805f]94}
95
[eb46fdf]96// Do we control where exceptions get thrown even with concurency?
97// If not these are not quite thread safe, the cleanup hook has to
98// be added after the node is built but before it is made the top node.
[b947fb2]99
[3090127]100void __cfaehm_try_resume_setup(struct __cfaehm_try_resume_node * node,
[0304215a]101 _Bool (*handler)(exception_t * except)) {
[2a3b019]102 struct exception_context_t * context = this_exception_context();
103 node->next = context->top_resume;
[307a732]104 node->handler = handler;
[2a3b019]105 context->top_resume = node;
[fa4805f]106}
107
[3090127]108void __cfaehm_try_resume_cleanup(struct __cfaehm_try_resume_node * node) {
[2a3b019]109 struct exception_context_t * context = this_exception_context();
110 context->top_resume = node->next;
[fa4805f]111}
112
113
[2922871]114// MEMORY MANAGEMENT =========================================================
[ff7ff14a]115
[980fb4e]116struct __cfaehm_node {
117 struct _Unwind_Exception unwind_exception;
118 struct __cfaehm_node * next;
119 int handler_index;
120};
121
122#define NODE_TO_EXCEPT(node) ((exception_t *)(1 + (node)))
123#define EXCEPT_TO_NODE(except) ((struct __cfaehm_node *)(except) - 1)
124#define UNWIND_TO_NODE(unwind) ((struct __cfaehm_node *)(unwind))
125#define NULL_MAP(map, ptr) ((ptr) ? (map(ptr)) : NULL)
126
[73530d9]127// How to clean up an exception in various situations.
128static void __cfaehm_exception_cleanup(
129 _Unwind_Reason_Code reason,
130 struct _Unwind_Exception * exception) {
131 switch (reason) {
132 case _URC_FOREIGN_EXCEPTION_CAUGHT:
133 // This one we could clean-up to allow cross-language exceptions.
134 case _URC_FATAL_PHASE1_ERROR:
135 case _URC_FATAL_PHASE2_ERROR:
136 default:
137 abort();
138 }
139}
140
[86d5ba7c]141// Creates a copy of the indicated exception and sets current_exception to it.
[3090127]142static void __cfaehm_allocate_exception( exception_t * except ) {
[86d5ba7c]143 struct exception_context_t * context = this_exception_context();
144
[ff7ff14a]145 // Allocate memory for the exception.
[3090127]146 struct __cfaehm_node * store = malloc(
147 sizeof( struct __cfaehm_node ) + except->virtual_table->size );
[ff7ff14a]148
149 if ( ! store ) {
150 // Failure: cannot allocate exception. Terminate thread.
151 abort(); // <- Although I think it might be the process.
[86d5ba7c]152 }
153
[980fb4e]154 // Initialize the node:
155 exception_t * except_store = NODE_TO_EXCEPT(store);
156 store->unwind_exception.exception_class = __cfaehm_exception_class;
157 store->unwind_exception.exception_cleanup = __cfaehm_exception_cleanup;
158 store->handler_index = 0;
159 except->virtual_table->copy( except_store, except );
[73530d9]160
[980fb4e]161 // Add the node to the list:
162 store->next = NULL_MAP(EXCEPT_TO_NODE, context->current_exception);
163 context->current_exception = except_store;
[86d5ba7c]164}
165
166// Delete the provided exception, unsetting current_exception if relivant.
[3090127]167static void __cfaehm_delete_exception( exception_t * except ) {
[86d5ba7c]168 struct exception_context_t * context = this_exception_context();
169
[851fd92]170 __cfadbg_print_safe(exception, "Deleting Exception\n");
[ff7ff14a]171
172 // Remove the exception from the list.
[3090127]173 struct __cfaehm_node * to_free = EXCEPT_TO_NODE(except);
174 struct __cfaehm_node * node;
[86d5ba7c]175
176 if ( context->current_exception == except ) {
[ff7ff14a]177 node = to_free->next;
[980fb4e]178 context->current_exception = NULL_MAP(NODE_TO_EXCEPT, node);
[86d5ba7c]179 } else {
[ff7ff14a]180 node = EXCEPT_TO_NODE(context->current_exception);
181 // It may always be in the first or second position.
[9cb89b87]182 while ( to_free != node->next ) {
[ff7ff14a]183 node = node->next;
184 }
185 node->next = to_free->next;
[86d5ba7c]186 }
[ff7ff14a]187
188 // Free the old exception node.
[cbce272]189 except->virtual_table->free( except );
[ff7ff14a]190 free( to_free );
[86d5ba7c]191}
192
[2922871]193// CANCELLATION ==============================================================
[fa4805f]194
195// Function needed by force unwind
196// It basically says to unwind the whole stack and then exit when we reach the end of the stack
197static _Unwind_Reason_Code _Stop_Fn(
198 int version,
199 _Unwind_Action actions,
[3090127]200 _Unwind_Exception_Class exception_class,
[fa4805f]201 struct _Unwind_Exception * unwind_exception,
[2a3b019]202 struct _Unwind_Context * unwind_context,
203 void * stop_param) {
[a201f7b]204 // Verify actions follow the rules we expect.
[d119d613]205 verify(actions & _UA_CLEANUP_PHASE);
206 verify(actions & _UA_FORCE_UNWIND);
207 verify(!(actions & _UA_SEARCH_PHASE));
208 verify(!(actions & _UA_HANDLER_FRAME));
[fa4805f]209
[a201f7b]210 if ( actions & _UA_END_OF_STACK ) {
[d119d613]211 abort();
[a201f7b]212 } else {
213 return _URC_NO_REASON;
214 }
[fa4805f]215}
216
[d119d613]217__attribute__((weak)) _Unwind_Reason_Code
218__cfaehm_cancellation_unwind( struct _Unwind_Exception * exception ) {
219 return _Unwind_ForcedUnwind( exception, _Stop_Fn, (void*)0x22 );
220}
[980fb4e]221
[2922871]222// Cancel the current stack, prefroming approprate clean-up and messaging.
[979df46]223void __cfaehm_cancel_stack( exception_t * exception ) {
[d119d613]224 __cfaehm_allocate_exception( exception );
225
226 struct exception_context_t * context = this_exception_context();
227 struct __cfaehm_node * node = EXCEPT_TO_NODE(context->current_exception);
228
229 // Preform clean-up of any extra active exceptions.
230 while ( node->next ) {
231 struct __cfaehm_node * to_free = node->next;
232 node->next = to_free->next;
233 exception_t * except = NODE_TO_EXCEPT( to_free );
234 except->virtual_table->free( except );
235 free( to_free );
236 }
237
[2922871]238 _Unwind_Reason_Code ret;
[d119d613]239 ret = __cfaehm_cancellation_unwind( &node->unwind_exception );
[2922871]240 printf("UNWIND ERROR %d after force unwind\n", ret);
241 abort();
242}
243
244
245// TERMINATION ===============================================================
246
247// If this isn't a rethrow (*except==0), delete the provided exception.
248void __cfaehm_cleanup_terminate( void * except ) {
249 if ( *(void**)except ) __cfaehm_delete_exception( *(exception_t **)except );
250}
251
[8ad5752]252static void __cfaehm_cleanup_default( exception_t ** except ) {
253 __cfaehm_delete_exception( *except );
254 *except = NULL;
255}
256
[86d5ba7c]257// The exception that is being thrown must already be stored.
[046a890]258static void __cfaehm_begin_unwind(void(*defaultHandler)(exception_t *)) {
[8ad5752]259 struct exception_context_t * context = this_exception_context();
260 if ( NULL == context->current_exception ) {
[86d5ba7c]261 printf("UNWIND ERROR missing exception in begin unwind\n");
262 abort();
263 }
[980fb4e]264 struct _Unwind_Exception * storage =
265 &EXCEPT_TO_NODE(context->current_exception)->unwind_exception;
[fa4805f]266
267 // Call stdlibc to raise the exception
[8ad5752]268 __cfadbg_print_safe(exception, "Begin unwinding (storage &p, context %p)\n", storage, context);
269 _Unwind_Reason_Code ret = _Unwind_RaiseException( storage );
[fa4805f]270
[eb46fdf]271 // If we reach here it means something happened. For resumption to work we need to find a way
272 // to return back to here. Most of them will probably boil down to setting a global flag and
273 // making the phase 1 either stop or fail. Causing an error on purpose may help avoiding
274 // unnecessary work but it might have some weird side effects. If we just pretend no handler
275 // was found that would work but may be expensive for no reason since we will always search
276 // the whole stack.
[fa4805f]277
[8ad5752]278 // We did not simply reach the end of the stack without finding a handler. This is an error.
279 if ( ret != _URC_END_OF_STACK ) {
280 printf("UNWIND ERROR %d after raise exception\n", ret);
281 abort();
[fa4805f]282 }
283
[8ad5752]284 // No handler found, go to the default operation.
285 __cfadbg_print_safe(exception, "Uncaught exception %p\n", storage);
286
287 __attribute__((cleanup(__cfaehm_cleanup_default)))
288 exception_t * exception = context->current_exception;
289 defaultHandler( exception );
[fa4805f]290}
291
[046a890]292void __cfaehm_throw_terminate( exception_t * val, void (*defaultHandler)(exception_t *) ) {
[851fd92]293 __cfadbg_print_safe(exception, "Throwing termination exception\n");
[86d5ba7c]294
[3090127]295 __cfaehm_allocate_exception( val );
[046a890]296 __cfaehm_begin_unwind( defaultHandler );
297}
298
299static __attribute__((noreturn)) void __cfaehm_rethrow_adapter( exception_t * except ) {
300 // TODO: Print some error message.
301 (void)except;
302 abort();
[86d5ba7c]303}
304
[3090127]305void __cfaehm_rethrow_terminate(void) {
[851fd92]306 __cfadbg_print_safe(exception, "Rethrowing termination exception\n");
[fa4805f]307
[046a890]308 __cfaehm_begin_unwind( __cfaehm_rethrow_adapter );
309 abort();
[fa4805f]310}
[ba70330]311
312#if defined( __x86_64 ) || defined( __i386 )
[eb46fdf]313// This is our personality routine. For every stack frame annotated with
314// ".cfi_personality 0x3,__gcfa_personality_v0" this function will be called twice when unwinding.
315// Once in the search phase and once in the cleanup phase.
[3090127]316_Unwind_Reason_Code __gcfa_personality_v0(
317 int version,
318 _Unwind_Action actions,
319 unsigned long long exception_class,
320 struct _Unwind_Exception * unwind_exception,
321 struct _Unwind_Context * unwind_context)
[fa4805f]322{
323
[851fd92]324 //__cfadbg_print_safe(exception, "CFA: 0x%lx\n", _Unwind_GetCFA(context));
325 __cfadbg_print_safe(exception, "Personality function (%d, %x, %llu, %p, %p):",
[3090127]326 version, actions, exception_class, unwind_exception, unwind_context);
[fa4805f]327
[a201f7b]328 // Verify that actions follow the rules we expect.
329 // This function should never be called at the end of the stack.
330 verify(!(actions & _UA_END_OF_STACK));
331 // Either only the search phase flag is set or...
[fa4805f]332 if (actions & _UA_SEARCH_PHASE) {
[a201f7b]333 verify(actions == _UA_SEARCH_PHASE);
[851fd92]334 __cfadbg_print_safe(exception, " lookup phase");
[a201f7b]335 // ... we are in clean-up phase.
336 } else {
337 verify(actions & _UA_CLEANUP_PHASE);
[851fd92]338 __cfadbg_print_safe(exception, " cleanup phase");
[a201f7b]339 // We shouldn't be the handler frame during forced unwind.
340 if (actions & _UA_HANDLER_FRAME) {
341 verify(!(actions & _UA_FORCE_UNWIND));
[851fd92]342 __cfadbg_print_safe(exception, " (handler frame)");
[a201f7b]343 } else if (actions & _UA_FORCE_UNWIND) {
[851fd92]344 __cfadbg_print_safe(exception, " (force unwind)");
[a201f7b]345 }
[fa4805f]346 }
347
348 // Get a pointer to the language specific data from which we will read what we need
[9cb89b87]349 const unsigned char * lsd = _Unwind_GetLanguageSpecificData( unwind_context );
[fa4805f]350
[9cb89b87]351 if ( !lsd ) { //Nothing to do, keep unwinding
[fa4805f]352 printf(" no LSD");
353 goto UNWIND;
354 }
355
356 // Get the instuction pointer and a reading pointer into the exception table
357 lsda_header_info lsd_info;
[2a3b019]358 const unsigned char * cur_ptr = parse_lsda_header(unwind_context, lsd, &lsd_info);
359 _Unwind_Ptr instruction_ptr = _Unwind_GetIP(unwind_context);
360
361 struct exception_context_t * context = this_exception_context();
[fa4805f]362
363 // Linearly search the table for stuff to do
[9cb89b87]364 while ( cur_ptr < lsd_info.action_table ) {
[fa4805f]365 _Unwind_Ptr callsite_start;
366 _Unwind_Ptr callsite_len;
367 _Unwind_Ptr callsite_landing_pad;
368 _uleb128_t callsite_action;
369
370 // Decode the common stuff we have in here
[eb46fdf]371 cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_start);
372 cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_len);
373 cur_ptr = read_encoded_value(0, lsd_info.call_site_encoding, cur_ptr, &callsite_landing_pad);
374 cur_ptr = read_uleb128(cur_ptr, &callsite_action);
[fa4805f]375
376 // Have we reach the correct frame info yet?
[9cb89b87]377 if ( lsd_info.Start + callsite_start + callsite_len < instruction_ptr ) {
[e9145a3]378#ifdef __CFA_DEBUG_PRINT__
[fa4805f]379 void * ls = (void*)lsd_info.Start;
380 void * cs = (void*)callsite_start;
381 void * cl = (void*)callsite_len;
382 void * bp = (void*)lsd_info.Start + callsite_start;
383 void * ep = (void*)lsd_info.Start + callsite_start + callsite_len;
384 void * ip = (void*)instruction_ptr;
[851fd92]385 __cfadbg_print_safe(exception, "\nfound %p - %p (%p, %p, %p), looking for %p\n",
[eb46fdf]386 bp, ep, ls, cs, cl, ip);
[e9145a3]387#endif // __CFA_DEBUG_PRINT__
[fa4805f]388 continue;
389 }
390
[eb46fdf]391 // Have we gone too far?
[9cb89b87]392 if ( lsd_info.Start + callsite_start > instruction_ptr ) {
[fa4805f]393 printf(" gone too far");
394 break;
395 }
396
[9cb89b87]397 // Check for what we must do:
[2a3b019]398 if ( 0 == callsite_landing_pad ) {
399 // Nothing to do, move along
[851fd92]400 __cfadbg_print_safe(exception, " no landing pad");
[9cb89b87]401 } else if (actions & _UA_SEARCH_PHASE) {
402 // In search phase, these means we found a potential handler we must check.
403
404 // We have arbitrarily decided that 0 means nothing to do and 1 means there is
405 // a potential handler. This doesn't seem to conflict the gcc default behavior.
406 if (callsite_action != 0) {
407 // Now we want to run some code to see if the handler matches
408 // This is the tricky part where we want to the power to run arbitrary code
409 // However, generating a new exception table entry and try routine every time
410 // is way more expansive than we might like
411 // The information we have is :
412 // - The GR (Series of registers)
413 // GR1=GP Global Pointer of frame ref by context
414 // - The instruction pointer
415 // - The instruction pointer info (???)
416 // - The CFA (Canonical Frame Address)
417 // - The BSP (Probably the base stack pointer)
418
419 // The current apprach uses one exception table entry per try block
420 _uleb128_t imatcher;
421 // Get the relative offset to the {...}?
422 cur_ptr = read_uleb128(cur_ptr, &imatcher);
423
[67ca73e]424 _Unwind_Word match_pos =
[9cb89b87]425# if defined( __x86_64 )
[67ca73e]426 _Unwind_GetCFA(unwind_context) + 8;
[9cb89b87]427# elif defined( __i386 )
[67ca73e]428 _Unwind_GetCFA(unwind_context) + 24;
429# elif defined( __ARM_ARCH )
430# warning FIX ME: check if anything needed for ARM
431 42;
[9cb89b87]432# endif
433 int (*matcher)(exception_t *) = *(int(**)(exception_t *))match_pos;
434
435 int index = matcher(context->current_exception);
436 _Unwind_Reason_Code ret = (0 == index)
437 ? _URC_CONTINUE_UNWIND : _URC_HANDLER_FOUND;
[980fb4e]438 UNWIND_TO_NODE(unwind_exception)->handler_index = index;
[9cb89b87]439
440 // Based on the return value, check if we matched the exception
441 if (ret == _URC_HANDLER_FOUND) {
[851fd92]442 __cfadbg_print_safe(exception, " handler found\n");
[9cb89b87]443 } else {
[980fb4e]444 // TODO: Continue the search if there is more in the table.
[851fd92]445 __cfadbg_print_safe(exception, " no handler\n");
[fa4805f]446 }
[9cb89b87]447 return ret;
[fa4805f]448 }
449
[9cb89b87]450 // This is only a cleanup handler, ignore it
[851fd92]451 __cfadbg_print_safe(exception, " no action");
[a201f7b]452 } else {
[9cb89b87]453 // In clean-up phase, no destructors here but this could be the handler.
454
455 if ( (callsite_action != 0) && !(actions & _UA_HANDLER_FRAME) ){
456 // If this is a potential exception handler
457 // but not the one that matched the exception in the seach phase,
458 // just ignore it
459 goto UNWIND;
460 }
[fa4805f]461
[9cb89b87]462 // We need to run some clean-up or a handler
463 // These statment do the right thing but I don't know any specifics at all
464 _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(0),
465 (_Unwind_Ptr)unwind_exception );
466 _Unwind_SetGR( unwind_context, __builtin_eh_return_data_regno(1), 0 );
[fa4805f]467
[9cb89b87]468 // I assume this sets the instruction pointer to the adress of the landing pad
469 // It doesn't actually set it, it only state the value that needs to be set once we
470 // return _URC_INSTALL_CONTEXT
471 _Unwind_SetIP( unwind_context, ((lsd_info.LPStart) + (callsite_landing_pad)) );
[fa4805f]472
[851fd92]473 __cfadbg_print_safe(exception, " action\n");
[fa4805f]474
[9cb89b87]475 // Return have some action to run
476 return _URC_INSTALL_CONTEXT;
[fa4805f]477 }
478 }
479 // No handling found
[851fd92]480 __cfadbg_print_safe(exception, " table end reached");
[fa4805f]481
482 UNWIND:
[851fd92]483 __cfadbg_print_safe(exception, " unwind\n");
[fa4805f]484
485 // Keep unwinding the stack
486 return _URC_CONTINUE_UNWIND;
487}
488
[0f6ac828]489#pragma GCC push_options
[f017af6]490#pragma GCC optimize(0)
[0f6ac828]491
[eb46fdf]492// Try statements are hoisted out see comments for details. While this could probably be unique
493// and simply linked from libcfa but there is one problem left, see the exception table for details
[fa4805f]494__attribute__((noinline))
[3090127]495void __cfaehm_try_terminate(void (*try_block)(),
[0304215a]496 void (*catch_block)(int index, exception_t * except),
497 __attribute__((unused)) int (*match_block)(exception_t * except)) {
[fa4805f]498 //! volatile int xy = 0;
499 //! printf("%p %p %p %p\n", &try_block, &catch_block, &match_block, &xy);
500
[eb46fdf]501 // Setup the personality routine and exception table.
[9cb89b87]502 // Unforturnately these clobber gcc cancellation support which means we can't get access to
503 // the attribute cleanup tables at the same time. We would have to inspect the assembly to
504 // create a new set ourselves.
[3b9c674]505#ifdef __PIC__
506 asm volatile (".cfi_personality 0x9b,CFA.ref.__gcfa_personality_v0");
507 asm volatile (".cfi_lsda 0x1b, .LLSDACFA2");
508#else
[fa4805f]509 asm volatile (".cfi_personality 0x3,__gcfa_personality_v0");
510 asm volatile (".cfi_lsda 0x3, .LLSDACFA2");
[3b9c674]511#endif
[fa4805f]512
[b947fb2]513 // Label which defines the start of the area for which the handler is setup.
[fa4805f]514 asm volatile (".TRYSTART:");
515
516 // The actual statements of the try blocks
517 try_block();
518
519 // asm statement to prevent deadcode removal
520 asm volatile goto ("" : : : : CATCH );
521
[eb46fdf]522 // Normal return for when there is no throw.
[fa4805f]523 return;
524
525 // Exceptionnal path
526 CATCH : __attribute__(( unused ));
[b947fb2]527 // Label which defines the end of the area for which the handler is setup.
[fa4805f]528 asm volatile (".TRYEND:");
[9cb89b87]529 // Label which defines the start of the exception landing pad. Basically what is called when
530 // the exception is caught. Note, if multiple handlers are given, the multiplexing should be
531 // done by the generated code, not the exception runtime.
[fa4805f]532 asm volatile (".CATCH:");
533
534 // Exception handler
[2a3b019]535 // Note: Saving the exception context on the stack breaks termination exceptions.
[980fb4e]536 catch_block( EXCEPT_TO_NODE( this_exception_context()->current_exception )->handler_index,
[2a3b019]537 this_exception_context()->current_exception );
[fa4805f]538}
539
[eb46fdf]540// Exception table data we need to generate. While this is almost generic, the custom data refers
541// to {*}try_terminate, which is no way generic. Some more works need to be done if we want to
542// have a single call to the try routine.
[b947fb2]543
[3b9c674]544#ifdef __PIC__
545asm (
546 // HEADER
547 ".LFECFA1:\n"
548 " .globl __gcfa_personality_v0\n"
549 " .section .gcc_except_table,\"a\",@progbits\n"
550 // TABLE HEADER (important field is the BODY length at the end)
551 ".LLSDACFA2:\n"
552 " .byte 0xff\n"
553 " .byte 0xff\n"
554 " .byte 0x1\n"
555 " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n"
556 // BODY (language specific data)
557 // This uses language specific data and can be modified arbitrarily
558 // We use handled area offset, handled area length,
559 // handler landing pad offset and 1 (action code, gcc seems to use 0).
560 ".LLSDACSBCFA2:\n"
[3090127]561 " .uleb128 .TRYSTART-__cfaehm_try_terminate\n"
[3b9c674]562 " .uleb128 .TRYEND-.TRYSTART\n"
[3090127]563 " .uleb128 .CATCH-__cfaehm_try_terminate\n"
[3b9c674]564 " .uleb128 1\n"
565 ".LLSDACSECFA2:\n"
566 // TABLE FOOTER
567 " .text\n"
[3090127]568 " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n"
[3b9c674]569);
570
571// Somehow this piece of helps with the resolution of debug symbols.
572__attribute__((unused)) static const int dummy = 0;
573
574asm (
575 // Add a hidden symbol which points at the function.
576 " .hidden CFA.ref.__gcfa_personality_v0\n"
577 " .weak CFA.ref.__gcfa_personality_v0\n"
578 // No clue what this does specifically
579 " .section .data.rel.local.CFA.ref.__gcfa_personality_v0,\"awG\",@progbits,CFA.ref.__gcfa_personality_v0,comdat\n"
580 " .align 8\n"
581 " .type CFA.ref.__gcfa_personality_v0, @object\n"
582 " .size CFA.ref.__gcfa_personality_v0, 8\n"
583 "CFA.ref.__gcfa_personality_v0:\n"
584#if defined( __x86_64 )
585 " .quad __gcfa_personality_v0\n"
586#else // then __i386
[e045590]587 " .long __gcfa_personality_v0\n"
[3b9c674]588#endif
589);
590#else // __PIC__
[fa4805f]591asm (
[eb46fdf]592 // HEADER
[fa4805f]593 ".LFECFA1:\n"
594 " .globl __gcfa_personality_v0\n"
595 " .section .gcc_except_table,\"a\",@progbits\n"
[eb46fdf]596 // TABLE HEADER (important field is the BODY length at the end)
597 ".LLSDACFA2:\n"
[fa4805f]598 " .byte 0xff\n"
599 " .byte 0xff\n"
600 " .byte 0x1\n"
[eb46fdf]601 " .uleb128 .LLSDACSECFA2-.LLSDACSBCFA2\n"
602 // BODY (language specific data)
603 ".LLSDACSBCFA2:\n"
604 // Handled area start (relative to start of function)
[3090127]605 " .uleb128 .TRYSTART-__cfaehm_try_terminate\n"
[eb46fdf]606 // Handled area length
607 " .uleb128 .TRYEND-.TRYSTART\n"
608 // Handler landing pad address (relative to start of function)
[3090127]609 " .uleb128 .CATCH-__cfaehm_try_terminate\n"
[eb46fdf]610 // Action code, gcc seems to always use 0.
611 " .uleb128 1\n"
612 // TABLE FOOTER
613 ".LLSDACSECFA2:\n"
614 " .text\n"
[3090127]615 " .size __cfaehm_try_terminate, .-__cfaehm_try_terminate\n"
[fa4805f]616 " .ident \"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901\"\n"
[3b9c674]617 " .section .note.GNU-stack,\"x\",@progbits\n"
[fa4805f]618);
[3b9c674]619#endif // __PIC__
620
621#pragma GCC pop_options
[915aa11]622
623#elif defined( __ARM_ARCH )
624_Unwind_Reason_Code __gcfa_personality_v0(
625 int version,
626 _Unwind_Action actions,
627 unsigned long long exception_class,
628 struct _Unwind_Exception * unwind_exception,
629 struct _Unwind_Context * unwind_context) {
630 return _URC_CONTINUE_UNWIND;
631}
632
633__attribute__((noinline))
634void __cfaehm_try_terminate(void (*try_block)(),
635 void (*catch_block)(int index, exception_t * except),
636 __attribute__((unused)) int (*match_block)(exception_t * except)) {
637}
638#else
639 #error unsupported hardware architecture
[ba70330]640#endif // __x86_64 || __i386
Note: See TracBrowser for help on using the repository browser.