source: libcfa/src/exception.c @ e8261bb

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e8261bb was e8261bb, checked in by Henry Xue <y58xue@…>, 3 years ago

Preliminary ARM exception handling support

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