source: libcfa/src/exception.c@ 980fb4e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 980fb4e was 980fb4e, checked in by Andrew Beach <ajbeach@…>, 6 years ago

Added a test for exceptions and made a patch to allow it to pass.

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