PR c++/86728 - C variadic generic lambda.
[official-gcc.git] / gcc / ada / raise-gcc.c
blob5c2cc43bcb3cfd21f071cdf5b94e404d5cb4e563
1 /****************************************************************************
2 * *
3 * GNAT COMPILER COMPONENTS *
4 * *
5 * R A I S E - G C C *
6 * *
7 * C Implementation File *
8 * *
9 * Copyright (C) 1992-2018, Free Software Foundation, Inc. *
10 * *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
17 * *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
21 * *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
26 * *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
29 * *
30 ****************************************************************************/
32 /* Code related to the integration of the GCC mechanism for exception
33 handling. */
35 #ifndef IN_RTS
36 /* For gnat1/gnatbind compilation: use host headers. */
37 # include "config.h"
38 # include "system.h"
39 /* Don't use fancy_abort. */
40 # undef abort
41 #else
42 # ifndef CERT
43 # include "tconfig.h"
44 # include "tsystem.h"
45 # else
46 # define ATTRIBUTE_UNUSED __attribute__((unused))
47 # define HAVE_GETIPINFO 1
48 # endif
49 #endif
51 #include <stdarg.h>
53 #ifdef __cplusplus
54 # include <cstdlib>
55 #else
56 typedef char bool;
57 # define true 1
58 # define false 0
59 #endif
61 #include "raise.h"
63 #ifdef __APPLE__
64 /* On MacOS X, versions older than 10.5 don't export _Unwind_GetIPInfo. */
65 #undef HAVE_GETIPINFO
66 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
67 #define HAVE_GETIPINFO 1
68 #endif
69 #endif
71 #if defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
72 /* HP-UX B.11.31 ia64 libunwind doesn't have _Unwind_GetIPInfo. */
73 #undef HAVE_GETIPINFO
74 #define _UA_END_OF_STACK 0
75 #endif
77 /* The names of a couple of "standard" routines for unwinding/propagation
78 actually vary depending on the underlying GCC scheme for exception handling
79 (SJLJ or DWARF). We need a consistently named interface to import from
80 a-except, so wrappers are defined here. */
82 #ifndef IN_RTS
83 /* For gnat1/gnatbind compilation: cannot use unwind.h, as it is for the
84 target. So mimic configure...
85 This is a hack ???, the real fix is to link gnat1/gnatbind with the
86 runtime of the build compiler. */
87 # ifdef EH_MECHANISM_arm
88 # include "config/arm/unwind-arm.h"
89 # else
90 # include "unwind-generic.h"
91 # endif
92 #else
93 # include "unwind.h"
94 #endif
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
100 typedef struct _Unwind_Context _Unwind_Context;
101 typedef struct _Unwind_Exception _Unwind_Exception;
103 _Unwind_Reason_Code
104 __gnat_Unwind_RaiseException (_Unwind_Exception *);
106 _Unwind_Reason_Code
107 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *, _Unwind_Stop_Fn, void *);
109 extern struct Exception_Occurrence *
110 __gnat_setup_current_excep (_Unwind_Exception *, _Unwind_Action);
112 extern void __gnat_unhandled_except_handler (_Unwind_Exception *);
114 #ifdef CERT
115 /* Called in case of error during propagation. */
116 extern void __gnat_raise_abort (void) __attribute__ ((noreturn));
117 #define abort() __gnat_raise_abort()
118 #endif
120 #include "unwind-pe.h"
122 #ifdef __ARM_EABI_UNWINDER__
123 /* for memcmp */
124 #include <string.h>
125 #endif
127 /* The known and handled exception classes. */
129 #ifdef __ARM_EABI_UNWINDER__
130 #define CXX_EXCEPTION_CLASS "GNUCC++"
131 #define GNAT_EXCEPTION_CLASS "GNU-Ada"
132 #else
133 #define CXX_EXCEPTION_CLASS 0x474e5543432b2b00ULL
134 #define GNAT_EXCEPTION_CLASS 0x474e552d41646100ULL
135 #endif
137 /* Structure of a C++ exception, represented as a C structure... See
138 unwind-cxx.h for the full definition. */
140 struct __cxa_exception
142 void *exceptionType;
143 void (*exceptionDestructor)(void *);
145 void (*unexpectedHandler)();
146 void (*terminateHandler)();
148 struct __cxa_exception *nextException;
150 int handlerCount;
152 #ifdef __ARM_EABI_UNWINDER__
153 struct __cxa_exception* nextPropagatingException;
155 int propagationCount;
156 #else
157 int handlerSwitchValue;
158 const unsigned char *actionRecord;
159 const unsigned char *languageSpecificData;
160 _Unwind_Ptr catchTemp;
161 void *adjustedPtr;
162 #endif
164 _Unwind_Exception unwindHeader;
167 /* --------------------------------------------------------------
168 -- The DB stuff below is there for debugging purposes only. --
169 -------------------------------------------------------------- */
171 #ifndef inhibit_libc
173 #define DB_PHASES 0x1
174 #define DB_CSITE 0x2
175 #define DB_ACTIONS 0x4
176 #define DB_REGIONS 0x8
178 #define DB_ERR 0x1000
180 /* The "action" stuff below is also there for debugging purposes only. */
182 typedef struct
184 _Unwind_Action phase;
185 const char * description;
186 } phase_descriptor;
188 static const phase_descriptor phase_descriptors[]
189 = {{ _UA_SEARCH_PHASE, "SEARCH_PHASE" },
190 { _UA_CLEANUP_PHASE, "CLEANUP_PHASE" },
191 { _UA_HANDLER_FRAME, "HANDLER_FRAME" },
192 { _UA_FORCE_UNWIND, "FORCE_UNWIND" },
193 { -1, 0}};
195 static int
196 db_accepted_codes (void)
198 static int accepted_codes = -1;
200 if (accepted_codes == -1)
202 char * db_env = (char *) getenv ("EH_DEBUG");
204 accepted_codes = db_env ? (atoi (db_env) | DB_ERR) : 0;
205 /* Arranged for ERR stuff to always be visible when the variable
206 is defined. One may just set the variable to 0 to see the ERR
207 stuff only. */
210 return accepted_codes;
213 #define DB_INDENT_INCREASE 0x01
214 #define DB_INDENT_DECREASE 0x02
215 #define DB_INDENT_OUTPUT 0x04
216 #define DB_INDENT_NEWLINE 0x08
217 #define DB_INDENT_RESET 0x10
219 #define DB_INDENT_UNIT 8
221 static void
222 db_indent (int requests)
224 static int current_indentation_level = 0;
226 if (requests & DB_INDENT_RESET)
227 current_indentation_level = 0;
229 if (requests & DB_INDENT_INCREASE)
230 current_indentation_level ++;
232 if (requests & DB_INDENT_DECREASE)
233 current_indentation_level --;
235 if (requests & DB_INDENT_NEWLINE)
236 fprintf (stderr, "\n");
238 if (requests & DB_INDENT_OUTPUT)
239 fprintf (stderr, "%*s", current_indentation_level * DB_INDENT_UNIT, " ");
242 static void ATTRIBUTE_PRINTF_2
243 db (int db_code, const char * msg_format, ...)
245 if (db_accepted_codes () & db_code)
247 va_list msg_args;
249 db_indent (DB_INDENT_OUTPUT);
251 va_start (msg_args, msg_format);
252 vfprintf (stderr, msg_format, msg_args);
253 va_end (msg_args);
257 static void
258 db_phases (int phases)
260 const phase_descriptor *a = phase_descriptors;
262 if (! (db_accepted_codes () & DB_PHASES))
263 return;
265 db (DB_PHASES, "\n");
267 for (; a->description != 0; a++)
268 if (phases & a->phase)
269 db (DB_PHASES, "%s ", a->description);
271 db (DB_PHASES, " :\n");
273 #else /* !inhibit_libc */
274 #define db_phases(X)
275 #define db_indent(X)
276 #define db(X, ...)
277 #endif /* !inhibit_libc */
279 /* ---------------------------------------------------------------
280 -- Now come a set of useful structures and helper routines. --
281 --------------------------------------------------------------- */
283 /* There are three major runtime tables involved, generated by the
284 GCC back-end. Contents slightly vary depending on the underlying
285 implementation scheme (dwarf zero cost / sjlj).
287 =======================================
288 * Tables for the dwarf zero cost case *
289 =======================================
291 They are fully documented in:
292 http://sourcery.mentor.com/public/cxx-abi/exceptions.pdf
293 Here is a shorter presentation, with some specific comments for Ada.
295 call_site []
296 -------------------------------------------------------------------
297 * region-start | region-length | landing-pad | first-action-index *
298 -------------------------------------------------------------------
300 Identify possible actions to be taken and where to resume control
301 for that when an exception propagates through a pc inside the region
302 delimited by start and length.
304 A null landing-pad indicates that nothing is to be done.
306 Otherwise, first-action-index provides an entry into the action[]
307 table which heads a list of possible actions to be taken (see below).
309 If it is determined that indeed an action should be taken, that
310 is, if one action filter matches the exception being propagated,
311 then control should be transferred to landing-pad.
313 A null first-action-index indicates that there are only cleanups
314 to run there.
316 action []
317 -------------------------------
318 * action-filter | next-action *
319 -------------------------------
321 This table contains lists (called action chains) of possible actions
322 associated with call-site entries described in the call-site [] table.
323 There is at most one action list per call-site entry. It is SLEB128
324 encoded.
326 A null action-filter indicates a cleanup.
328 Non null action-filters provide an index into the ttypes [] table
329 (see below), from which information may be retrieved to check if it
330 matches the exception being propagated.
332 * action-filter > 0:
333 means there is a regular handler to be run The value is also passed
334 to the landing pad to dispatch the exception.
336 * action-filter < 0:
337 means there is a some "exception_specification" data to retrieve,
338 which is only relevant for C++ and should never show up for Ada.
339 (Exception specification specifies which exceptions can be thrown
340 by a function. Such filter is emitted around the body of C++
341 functions defined like:
342 void foo ([...]) throw (A, B) { [...] }
343 These can be viewed as negativ filter: the landing pad is branched
344 to for exceptions that doesn't match the filter and usually aborts
345 the program).
347 * next-action
348 points to the next entry in the list using a relative byte offset. 0
349 indicates there is no other entry.
351 ttypes []
352 ---------------
353 * ttype-value *
354 ---------------
356 This table is an array of addresses.
358 A null value indicates a catch-all handler. (Not used by Ada)
360 Non null values are used to match the exception being propagated:
361 In C++ this is a pointer to some rtti data, while in Ada this is an
362 exception id (with a fake id for others).
364 For C++, this table is actually also used to store "exception
365 specification" data. The differentiation between the two kinds
366 of entries is made by the sign of the associated action filter,
367 which translates into positive or negative offsets from the
368 so called base of the table:
370 Exception Specification data is stored at positive offsets from
371 the ttypes table base, which Exception Type data is stored at
372 negative offsets:
374 ---------------------------------------------------------------------------
376 Here is a quick summary of the tables organization:
378 +-- Unwind_Context (pc, ...)
380 |(pc)
382 | CALL-SITE[]
384 | +=============================================================+
385 | | region-start + length | landing-pad | first-action-index |
386 | +=============================================================+
387 +-> | pc range 0 => no-action 0 => cleanups only |
388 | !0 => jump @ N --+ |
389 +====================================================== | ====+
392 ACTION [] |
394 +==========================================================+ |
395 | action-filter | next-action | |
396 +==========================================================+ |
397 | 0 => cleanup | |
398 | >0 => ttype index for handler ------+ 0 => end of chain | <-+
399 | <0 => ttype index for spec data | |
400 +==================================== | ===================+
403 TTYPES [] |
404 | Offset negated from
405 +=====================+ | the actual base.
406 | ttype-value | |
407 +============+=====================+ |
408 | | ... | |
409 | ... | exception id | <---+
410 | | ... |
411 | handlers +---------------------+
412 | | ... |
413 | ... | ... |
414 | | ... |
415 +============+=====================+ <<------ Table base
416 | ... | ... |
417 | specs | ... | (should not see negative filter
418 | ... | ... | values for Ada).
419 +============+=====================+
422 ============================
423 * Tables for the sjlj case *
424 ============================
426 So called "function contexts" are pushed on a context stack by calls to
427 _Unwind_SjLj_Register on function entry, and popped off at exit points by
428 calls to _Unwind_SjLj_Unregister. The current call_site for a function is
429 updated in the function context as the function's code runs along.
431 The generic unwinding engine in _Unwind_RaiseException walks the function
432 context stack and not the actual call chain.
434 The ACTION and TTYPES tables remain unchanged, which allows to search them
435 during the propagation phase to determine whether or not the propagated
436 exception is handled somewhere. When it is, we only "jump" up once directly
437 to the context where the handler will be found. Besides, this allows "break
438 exception unhandled" to work also
440 The CALL-SITE table is setup differently, though: the pc attached to the
441 unwind context is a direct index into the table, so the entries in this
442 table do not hold region bounds any more.
444 A special index (-1) is used to indicate that no action is possibly
445 connected with the context at hand, so null landing pads cannot appear
446 in the table.
448 Additionally, landing pad values in the table do not represent code address
449 to jump at, but so called "dispatch" indices used by a common landing pad
450 for the function to switch to the appropriate post-landing-pad.
452 +-- Unwind_Context (pc, ...)
454 | pc = call-site index
455 | 0 => terminate (should not see this for Ada)
456 | -1 => no-action
458 | CALL-SITE[]
460 | +=====================================+
461 | | landing-pad | first-action-index |
462 | +=====================================+
463 +-> | 0 => cleanups only |
464 | dispatch index N |
465 +=====================================+
468 ===================================
469 * Basic organization of this unit *
470 ===================================
472 The major point of this unit is to provide an exception propagation
473 personality routine for Ada. This is __gnat_personality_v0.
475 It is provided with a pointer to the propagated exception, an unwind
476 context describing a location the propagation is going through, and a
477 couple of other arguments including a description of the current
478 propagation phase.
480 It shall return to the generic propagation engine what is to be performed
481 next, after possible context adjustments, depending on what it finds in the
482 traversed context (a handler for the exception, a cleanup, nothing, ...),
483 and on the propagation phase.
485 A number of structures and subroutines are used for this purpose, as
486 sketched below:
488 o region_descriptor: General data associated with the context (base pc,
489 call-site table, action table, ttypes table, ...)
491 o action_descriptor: Data describing the action to be taken for the
492 propagated exception in the provided context (kind of action: nothing,
493 handler, cleanup; pointer to the action table entry, ...).
495 raise
497 ... (a-except.adb)
499 Propagate_Exception (a-exexpr.adb)
502 _Unwind_RaiseException (libgcc)
504 | (Ada frame)
506 +--> __gnat_personality_v0 (context, exception)
508 +--> get_region_description_for (context)
510 +--> get_action_description_for (ip, exception, region)
512 | +--> get_call_site_action_for (context, region)
513 | (one version for each underlying scheme)
515 +--> setup_to_install (context)
517 This unit is inspired from the C++ version found in eh_personality.cc,
518 part of libstdc++-v3.
523 /* This is an incomplete "proxy" of the structure of exception objects as
524 built by the GNAT runtime library. Accesses to other fields than the common
525 header are performed through subprogram calls to alleviate the need of an
526 exact counterpart here and potential alignment/size issues for the common
527 header. See a-exexpr.adb. */
529 typedef struct
531 _Unwind_Exception common;
532 /* ABI header, maximally aligned. */
533 } _GNAT_Exception;
535 /* The two constants below are specific ttype identifiers for special
536 exception ids. Their type should match what a-exexpr exports. */
538 extern const int __gnat_others_value;
539 #define GNAT_OTHERS ((_Unwind_Ptr) &__gnat_others_value)
541 extern const int __gnat_all_others_value;
542 #define GNAT_ALL_OTHERS ((_Unwind_Ptr) &__gnat_all_others_value)
544 extern const int __gnat_unhandled_others_value;
545 #define GNAT_UNHANDLED_OTHERS ((_Unwind_Ptr) &__gnat_unhandled_others_value)
547 /* Describe the useful region data associated with an unwind context. */
549 typedef struct
551 /* The base pc of the region. */
552 _Unwind_Ptr base;
554 /* Pointer to the Language Specific Data for the region. */
555 _Unwind_Ptr lsda;
557 /* Call-Site data associated with this region. */
558 unsigned char call_site_encoding;
559 const unsigned char *call_site_table;
561 /* The base to which are relative landing pad offsets inside the call-site
562 entries . */
563 _Unwind_Ptr lp_base;
565 /* Action-Table associated with this region. */
566 const unsigned char *action_table;
568 /* Ttype data associated with this region. */
569 unsigned char ttype_encoding;
570 const unsigned char *ttype_table;
571 _Unwind_Ptr ttype_base;
573 } region_descriptor;
575 /* Extract and adjust the IP (instruction pointer) from an exception
576 context. */
578 static _Unwind_Ptr
579 get_ip_from_context (_Unwind_Context *uw_context)
581 int ip_before_insn = 0;
582 #ifdef HAVE_GETIPINFO
583 _Unwind_Ptr ip = _Unwind_GetIPInfo (uw_context, &ip_before_insn);
584 #else
585 _Unwind_Ptr ip = _Unwind_GetIP (uw_context);
586 #endif
587 /* Subtract 1 if necessary because GetIPInfo yields a call return address
588 in this case, while we are interested in information for the call point.
589 This does not always yield the exact call instruction address but always
590 brings the IP back within the corresponding region. */
591 if (!ip_before_insn)
592 ip--;
594 return ip;
597 static void
598 db_region_for (region_descriptor *region, _Unwind_Ptr ip)
600 #ifndef inhibit_libc
601 if (! (db_accepted_codes () & DB_REGIONS))
602 return;
604 db (DB_REGIONS, "For ip @ %p => ", (void *)ip);
606 if (region->lsda)
607 db (DB_REGIONS, "lsda @ %p", (void *)region->lsda);
608 else
609 db (DB_REGIONS, "no lsda");
611 db (DB_REGIONS, "\n");
612 #endif
615 /* Retrieve the ttype entry associated with FILTER in the REGION's
616 ttype table. */
618 static _Unwind_Ptr
619 get_ttype_entry_for (region_descriptor *region, long filter)
621 _Unwind_Ptr ttype_entry;
623 filter *= size_of_encoded_value (region->ttype_encoding);
624 read_encoded_value_with_base
625 (region->ttype_encoding, region->ttype_base,
626 region->ttype_table - filter, &ttype_entry);
628 return ttype_entry;
631 /* Fill out the REGION descriptor for the provided UW_CONTEXT. */
633 static void
634 get_region_description_for (_Unwind_Context *uw_context,
635 region_descriptor *region)
637 const unsigned char * p;
638 _uleb128_t tmp;
639 unsigned char lpbase_encoding;
641 /* Get the base address of the lsda information. If the provided context
642 is null or if there is no associated language specific data, there's
643 nothing we can/should do. */
644 region->lsda
645 = (_Unwind_Ptr) (uw_context
646 ? _Unwind_GetLanguageSpecificData (uw_context) : 0);
648 if (! region->lsda)
649 return;
651 /* Parse the lsda and fill the region descriptor. */
652 p = (const unsigned char *)region->lsda;
654 region->base = _Unwind_GetRegionStart (uw_context);
656 /* Find @LPStart, the base to which landing pad offsets are relative. */
657 lpbase_encoding = *p++;
658 if (lpbase_encoding != DW_EH_PE_omit)
659 p = read_encoded_value
660 (uw_context, lpbase_encoding, p, &region->lp_base);
661 else
662 region->lp_base = region->base;
664 /* Find @TType, the base of the handler and exception spec type data. */
665 region->ttype_encoding = *p++;
666 if (region->ttype_encoding != DW_EH_PE_omit)
668 p = read_uleb128 (p, &tmp);
669 region->ttype_table = p + tmp;
671 else
672 region->ttype_table = 0;
674 region->ttype_base
675 = base_of_encoded_value (region->ttype_encoding, uw_context);
677 /* Get the encoding and length of the call-site table; the action table
678 immediately follows. */
679 region->call_site_encoding = *p++;
680 region->call_site_table = read_uleb128 (p, &tmp);
682 region->action_table = region->call_site_table + tmp;
686 /* Describe an action to be taken when propagating an exception up to
687 some context. */
689 enum action_kind
691 /* Found some call site base data, but need to analyze further
692 before being able to decide. */
693 unknown,
695 /* There is nothing relevant in the context at hand. */
696 nothing,
698 /* There are only cleanups to run in this context. */
699 cleanup,
701 /* There is a handler for the exception in this context. */
702 handler,
704 /* There is a handler for the exception, but it is only for catching
705 unhandled exceptions. */
706 unhandler
709 /* filter value for cleanup actions. */
710 static const int cleanup_filter = 0;
712 typedef struct
714 /* The kind of action to be taken. */
715 enum action_kind kind;
717 /* A pointer to the action record entry. */
718 const unsigned char *table_entry;
720 /* Where we should jump to actually take an action (trigger a cleanup or an
721 exception handler). */
722 _Unwind_Ptr landing_pad;
724 /* If we have a handler matching our exception, these are the filter to
725 trigger it and the corresponding id. */
726 _Unwind_Sword ttype_filter;
728 } action_descriptor;
730 static void
731 db_action_for (action_descriptor *action, _Unwind_Ptr ip)
733 #ifndef inhibit_libc
734 db (DB_ACTIONS, "For ip @ %p => ", (void *)ip);
736 switch (action->kind)
738 case unknown:
739 db (DB_ACTIONS, "lpad @ %p, record @ %p\n",
740 (void *) action->landing_pad, action->table_entry);
741 break;
743 case nothing:
744 db (DB_ACTIONS, "Nothing\n");
745 break;
747 case cleanup:
748 db (DB_ACTIONS, "Cleanup\n");
749 break;
751 case handler:
752 db (DB_ACTIONS, "Handler, filter = %d\n", (int) action->ttype_filter);
753 break;
755 default:
756 db (DB_ACTIONS, "Err? Unexpected action kind !\n");
757 break;
759 #endif
762 /* Search the call_site_table of REGION for an entry appropriate for the
763 UW_CONTEXT's IP. If one is found, store the associated landing_pad
764 and action_table entry, and set the ACTION kind to unknown for further
765 analysis. Otherwise, set the ACTION kind to nothing.
767 There are two variants of this routine, depending on the underlying
768 mechanism (DWARF/SJLJ), which account for differences in the tables. */
770 #ifdef __USING_SJLJ_EXCEPTIONS__
772 #define __builtin_eh_return_data_regno(x) x
774 static void
775 get_call_site_action_for (_Unwind_Ptr call_site,
776 region_descriptor *region,
777 action_descriptor *action)
779 /* call_site is a direct index into the call-site table, with two special
780 values : -1 for no-action and 0 for "terminate". The latter should never
781 show up for Ada. To test for the former, beware that _Unwind_Ptr might
782 be unsigned. */
784 if ((int)call_site < 0)
786 action->kind = nothing;
788 else if (call_site == 0)
790 db (DB_ERR, "========> Err, null call_site for Ada/sjlj\n");
791 action->kind = nothing;
793 else
795 _uleb128_t cs_lp, cs_action;
796 const unsigned char *p;
798 /* Let the caller know there may be an action to take, but let it
799 determine the kind. */
800 action->kind = unknown;
802 /* We have a direct index into the call-site table, but this table is
803 made of leb128 values, the encoding length of which is variable. We
804 can't merely compute an offset from the index, then, but have to read
805 all the entries before the one of interest. */
806 p = region->call_site_table;
809 p = read_uleb128 (p, &cs_lp);
810 p = read_uleb128 (p, &cs_action);
812 while (--call_site);
814 action->landing_pad = cs_lp + 1;
816 if (cs_action)
817 action->table_entry = region->action_table + cs_action - 1;
818 else
819 action->table_entry = 0;
823 #else /* !__USING_SJLJ_EXCEPTIONS__ */
825 static void
826 get_call_site_action_for (_Unwind_Ptr ip,
827 region_descriptor *region,
828 action_descriptor *action)
830 const unsigned char *p = region->call_site_table;
832 /* Unless we are able to determine otherwise... */
833 action->kind = nothing;
835 db (DB_CSITE, "\n");
837 while (p < region->action_table)
839 _Unwind_Ptr cs_start, cs_len, cs_lp;
840 _uleb128_t cs_action;
842 /* Note that all call-site encodings are "absolute" displacements. */
843 p = read_encoded_value (0, region->call_site_encoding, p, &cs_start);
844 p = read_encoded_value (0, region->call_site_encoding, p, &cs_len);
845 p = read_encoded_value (0, region->call_site_encoding, p, &cs_lp);
846 p = read_uleb128 (p, &cs_action);
848 db (DB_CSITE,
849 "c_site @ %p (+%p), len = %p, lpad @ %p (+%p)\n",
850 (char *)region->base + cs_start, (void *)cs_start, (void *)cs_len,
851 (char *)region->lp_base + cs_lp, (void *)cs_lp);
853 /* The table is sorted, so if we've passed the IP, stop. */
854 if (ip < region->base + cs_start)
855 break;
857 /* If we have a match, fill the ACTION fields accordingly. */
858 else if (ip < region->base + cs_start + cs_len)
860 /* Let the caller know there may be an action to take, but let it
861 determine the kind. */
862 action->kind = unknown;
864 if (cs_lp)
865 action->landing_pad = region->lp_base + cs_lp;
866 else
867 action->landing_pad = 0;
869 if (cs_action)
870 action->table_entry = region->action_table + cs_action - 1;
871 else
872 action->table_entry = 0;
874 db (DB_CSITE, "+++\n");
875 return;
879 db (DB_CSITE, "---\n");
882 #endif /* __USING_SJLJ_EXCEPTIONS__ */
884 /* With CHOICE an exception choice representing an "exception - when"
885 argument, and PROPAGATED_EXCEPTION a pointer to the currently propagated
886 occurrence, return true if the latter matches the former, that is, if
887 PROPAGATED_EXCEPTION is caught by the handling code controlled by CHOICE.
888 This takes care of the special Non_Ada_Error case on VMS. */
890 #define Is_Handled_By_Others __gnat_is_handled_by_others
891 #define Language_For __gnat_language_for
892 #define Foreign_Data_For __gnat_foreign_data_for
893 #define EID_For __gnat_eid_for
895 extern bool Is_Handled_By_Others (_Unwind_Ptr eid);
896 extern char Language_For (_Unwind_Ptr eid);
898 extern void *Foreign_Data_For (_Unwind_Ptr eid);
900 extern Exception_Id EID_For (_GNAT_Exception * e);
902 #define Foreign_Exception system__exceptions__foreign_exception
903 extern struct Exception_Data Foreign_Exception;
905 #ifdef VMS
906 #define Non_Ada_Error system__aux_dec__non_ada_error
907 extern struct Exception_Data Non_Ada_Error;
908 #endif
910 /* Return true iff the exception class of EXCEPT is EC. */
912 static int
913 exception_class_eq (const _GNAT_Exception *except,
914 const _Unwind_Exception_Class ec)
916 #ifdef __ARM_EABI_UNWINDER__
917 return memcmp (except->common.exception_class, ec, 8) == 0;
918 #else
919 return except->common.exception_class == ec;
920 #endif
923 /* Return how CHOICE matches PROPAGATED_EXCEPTION. */
925 static enum action_kind
926 is_handled_by (_Unwind_Ptr choice, _GNAT_Exception *propagated_exception)
928 /* All others choice match everything. */
929 if (choice == GNAT_ALL_OTHERS)
930 return handler;
932 /* GNAT exception occurrence. */
933 if (exception_class_eq (propagated_exception, GNAT_EXCEPTION_CLASS))
935 /* Pointer to the GNAT exception data corresponding to the propagated
936 occurrence. */
937 _Unwind_Ptr E = (_Unwind_Ptr) EID_For (propagated_exception);
939 if (choice == GNAT_UNHANDLED_OTHERS)
940 return unhandler;
942 E = (_Unwind_Ptr) EID_For (propagated_exception);
944 /* Base matching rules: An exception data (id) matches itself, "when
945 all_others" matches anything and "when others" matches anything
946 unless explicitly stated otherwise in the propagated occurrence. */
947 if (choice == E || (choice == GNAT_OTHERS && Is_Handled_By_Others (E)))
948 return handler;
950 #ifdef VMS
951 /* In addition, on OpenVMS, Non_Ada_Error matches VMS exceptions, and we
952 may have different exception data pointers that should match for the
953 same condition code, if both an export and an import have been
954 registered. The import code for both the choice and the propagated
955 occurrence are expected to have been masked off regarding severity
956 bits already (at registration time for the former and from within the
957 low level exception vector for the latter). */
958 if ((Language_For (E) == 'V'
959 && choice != GNAT_OTHERS
960 && ((Language_For (choice) == 'V'
961 && Foreign_Data_For (choice) != 0
962 && Foreign_Data_For (choice) == Foreign_Data_For (E))
963 || choice == (_Unwind_Ptr)&Non_Ada_Error)))
964 return handler;
965 #endif
967 /* Otherwise, it doesn't match an Ada choice. */
968 return nothing;
971 /* All others and others choice match any foreign exception. */
972 if (choice == GNAT_ALL_OTHERS
973 || choice == GNAT_OTHERS
974 #ifndef CERT
975 || choice == (_Unwind_Ptr) &Foreign_Exception
976 #endif
978 return handler;
980 #ifndef CERT
981 /* C++ exception occurrences. */
982 if (exception_class_eq (propagated_exception, CXX_EXCEPTION_CLASS)
983 && Language_For (choice) == 'C')
985 void *choice_typeinfo = Foreign_Data_For (choice);
986 void *except_typeinfo =
987 (((struct __cxa_exception *)
988 ((_Unwind_Exception *)propagated_exception + 1)) - 1)
989 ->exceptionType;
991 /* Typeinfo are directly compared, which might not be correct if they
992 aren't merged. ??? We should call the == operator if this module is
993 compiled in C++. */
994 if (choice_typeinfo == except_typeinfo)
995 return handler;
997 #endif
999 return nothing;
1002 /* Fill out the ACTION to be taken from propagating UW_EXCEPTION up to
1003 UW_CONTEXT in REGION. */
1005 static void
1006 get_action_description_for (_Unwind_Ptr ip,
1007 _Unwind_Exception *uw_exception,
1008 _Unwind_Action uw_phase,
1009 region_descriptor *region,
1010 action_descriptor *action)
1012 _GNAT_Exception *gnat_exception = (_GNAT_Exception *) uw_exception;
1014 /* Search the call site table first, which may get us a landing pad as well
1015 as the head of an action record list. */
1016 get_call_site_action_for (ip, region, action);
1017 db_action_for (action, ip);
1019 /* If there is not even a call_site entry, we are done. */
1020 if (action->kind == nothing)
1021 return;
1023 /* Otherwise, check what we have at the place of the call site. */
1025 /* No landing pad => no cleanups or handlers. */
1026 if (action->landing_pad == 0)
1028 action->kind = nothing;
1029 return;
1032 /* Landing pad + null table entry => only cleanups. */
1033 else if (action->table_entry == 0)
1035 action->kind = cleanup;
1036 action->ttype_filter = cleanup_filter;
1037 /* The filter initialization is not strictly necessary, as cleanup-only
1038 landing pads don't look at the filter value. It is there to ensure
1039 we don't pass random values and so trigger potential confusion when
1040 installing the context later on. */
1041 return;
1044 /* Landing pad + Table entry => handlers + possible cleanups. */
1045 else
1047 const unsigned char * p = action->table_entry;
1048 _sleb128_t ar_filter, ar_disp;
1050 action->kind = nothing;
1052 while (1)
1054 p = read_sleb128 (p, &ar_filter);
1055 read_sleb128 (p, &ar_disp);
1056 /* Don't assign p here, as it will be incremented by ar_disp
1057 below. */
1059 /* Null filters are for cleanups. */
1060 if (ar_filter == cleanup_filter)
1062 action->kind = cleanup;
1063 action->ttype_filter = cleanup_filter;
1064 /* The filter initialization is required here, to ensure
1065 the target landing pad branches to the cleanup code if
1066 we happen not to find a matching handler. */
1069 /* Positive filters are for regular handlers. */
1070 else if (ar_filter > 0)
1072 /* Do not catch an exception if the _UA_FORCE_UNWIND flag is
1073 passed (to follow the ABI). */
1074 if (!(uw_phase & _UA_FORCE_UNWIND))
1076 enum action_kind act;
1078 /* See if the filter we have is for an exception which
1079 matches the one we are propagating. */
1080 _Unwind_Ptr choice =
1081 get_ttype_entry_for (region, ar_filter);
1083 act = is_handled_by (choice, gnat_exception);
1084 if (act != nothing)
1086 action->kind = act;
1087 action->ttype_filter = ar_filter;
1088 return;
1093 /* Negative filter values are for C++ exception specifications.
1094 Should not be there for Ada :/ */
1095 else
1096 db (DB_ERR, "========> Err, filter < 0 for Ada/dwarf\n");
1098 if (ar_disp == 0)
1099 return;
1101 p += ar_disp;
1106 /* Setup in UW_CONTEXT the eh return target IP and data registers, which will
1107 be restored with the others and retrieved by the landing pad once the jump
1108 occurred. */
1110 static void
1111 setup_to_install (_Unwind_Context *uw_context,
1112 _Unwind_Exception *uw_exception,
1113 _Unwind_Ptr uw_landing_pad,
1114 int uw_filter)
1116 /* 1/ exception object pointer, which might be provided back to
1117 _Unwind_Resume (and thus to this personality routine) if we are jumping
1118 to a cleanup. */
1119 _Unwind_SetGR (uw_context, __builtin_eh_return_data_regno (0),
1120 (_Unwind_Word)uw_exception);
1122 /* 2/ handler switch value register, which will also be used by the target
1123 landing pad to decide what action it shall take. */
1124 _Unwind_SetGR (uw_context, __builtin_eh_return_data_regno (1),
1125 (_Unwind_Word)uw_filter);
1127 /* Setup the address we should jump at to reach the code where there is the
1128 "something" we found. */
1129 _Unwind_SetIP (uw_context, uw_landing_pad);
1132 /* The following is defined from a-except.adb. Its purpose is to enable
1133 automatic backtraces upon exception raise, as provided through the
1134 GNAT.Traceback facilities. */
1135 extern void __gnat_notify_handled_exception (struct Exception_Occurrence *);
1136 extern void __gnat_notify_unhandled_exception (struct Exception_Occurrence *);
1138 /* Below is the eh personality routine per se. We currently assume that only
1139 GNU-Ada exceptions are met. */
1141 /* By default, the personality routine is public. */
1142 #define PERSONALITY_STORAGE
1144 #ifdef __USING_SJLJ_EXCEPTIONS__
1145 #define PERSONALITY_FUNCTION __gnat_personality_sj0
1146 #elif defined (__SEH__)
1147 #define PERSONALITY_FUNCTION __gnat_personality_imp
1148 /* The public personality routine for seh is __gnat_personality_seh0, defined
1149 below using the SEH convention. This is a wrapper around the GNU routine,
1150 which is static. */
1151 #undef PERSONALITY_STORAGE
1152 #define PERSONALITY_STORAGE static
1153 #else
1154 #define PERSONALITY_FUNCTION __gnat_personality_v0
1155 #endif
1157 /* Code executed to continue unwinding. With the ARM unwinder, the
1158 personality routine must unwind one frame (per EHABI 7.3 4.). */
1160 static _Unwind_Reason_Code
1161 continue_unwind (struct _Unwind_Exception* ue_header ATTRIBUTE_UNUSED,
1162 struct _Unwind_Context* uw_context ATTRIBUTE_UNUSED)
1164 #ifdef __ARM_EABI_UNWINDER__
1165 if (__gnu_unwind_frame (ue_header, uw_context) != _URC_OK)
1166 return _URC_FAILURE;
1167 #endif
1168 return _URC_CONTINUE_UNWIND;
1171 /* Common code for the body of GNAT personality routine. This code is shared
1172 between all unwinders. */
1174 static _Unwind_Reason_Code
1175 personality_body (_Unwind_Action uw_phases,
1176 _Unwind_Exception *uw_exception,
1177 _Unwind_Context *uw_context)
1179 region_descriptor region;
1180 action_descriptor action;
1181 _Unwind_Ptr ip;
1183 /* Debug traces. */
1184 db_indent (DB_INDENT_RESET);
1185 db_phases (uw_phases);
1186 db_indent (DB_INDENT_INCREASE);
1188 /* Get the region description for the context we were provided with. This
1189 will tell us if there is some lsda, call_site, action and/or ttype data
1190 for the associated ip. */
1191 get_region_description_for (uw_context, &region);
1193 /* No LSDA => no handlers or cleanups => we shall unwind further up. */
1194 if (! region.lsda)
1195 return continue_unwind (uw_exception, uw_context);
1197 /* Get the instruction pointer. */
1198 ip = get_ip_from_context (uw_context);
1199 db_region_for (&region, ip);
1201 /* Search the call-site and action-record tables for the action associated
1202 with this IP. */
1203 get_action_description_for (ip, uw_exception, uw_phases, &region, &action);
1204 db_action_for (&action, ip);
1206 /* Whatever the phase, if there is nothing relevant in this frame,
1207 unwinding should just go on. */
1208 if (action.kind == nothing)
1209 return continue_unwind (uw_exception, uw_context);
1211 /* If we found something in search phase, we should return a code indicating
1212 what to do next depending on what we found. If we only have cleanups
1213 around, we shall try to unwind further up to find a handler, otherwise,
1214 tell we have a handler, which will trigger the second phase. */
1215 if (uw_phases & _UA_SEARCH_PHASE)
1217 if (action.kind == cleanup)
1219 return continue_unwind (uw_exception, uw_context);
1221 else
1223 #ifndef CERT
1224 /* Trigger the appropriate notification routines before the second
1225 phase starts, when the stack is still intact. First install what
1226 needs to be installed in the current exception buffer and fetch
1227 the Ada occurrence pointer to use. */
1229 struct Exception_Occurrence *excep
1230 = __gnat_setup_current_excep (uw_exception, uw_phases);
1232 if (action.kind == unhandler)
1233 __gnat_notify_unhandled_exception (excep);
1234 else
1235 __gnat_notify_handled_exception (excep);
1236 #endif
1238 return _URC_HANDLER_FOUND;
1242 /* We found something in cleanup/handler phase, which might be the handler
1243 or a cleanup for a handled occurrence, or a cleanup for an unhandled
1244 occurrence (we are in a FORCED_UNWIND phase in this case). Install the
1245 context to get there. */
1247 setup_to_install
1248 (uw_context, uw_exception, action.landing_pad, action.ttype_filter);
1250 #ifndef CERT
1251 /* Write current exception so that it can be retrieved from Ada. It was
1252 already done during phase 1, but one or several exceptions may have been
1253 raised in cleanup handlers in between. */
1254 __gnat_setup_current_excep (uw_exception, uw_phases);
1255 #endif
1257 return _URC_INSTALL_CONTEXT;
1260 #ifndef __ARM_EABI_UNWINDER__
1261 /* Major tweak for ia64-vms : the CHF propagation phase calls this personality
1262 routine with sigargs/mechargs arguments and has very specific expectations
1263 on possible return values.
1265 We handle this with a number of specific tricks:
1267 1. We tweak the personality routine prototype to have the "version" and
1268 "phases" two first arguments be void * instead of int and _Unwind_Action
1269 as nominally expected in the GCC context.
1271 This allows us to access the full range of bits passed in every case and
1272 has no impact on the callers side since each argument remains assigned
1273 the same single 64bit slot.
1275 2. We retrieve the corresponding int and _Unwind_Action values within the
1276 routine for regular use with truncating conversions. This is a noop when
1277 called from the libgcc unwinder.
1279 3. We assume we're called by the VMS CHF when unexpected bits are set in
1280 both those values. The incoming arguments are then real sigargs and
1281 mechargs pointers, which we then redirect to __gnat_handle_vms_condition
1282 for proper processing.
1284 #if defined (VMS) && defined (__IA64)
1285 typedef void * version_arg_t;
1286 typedef void * phases_arg_t;
1287 #else
1288 typedef int version_arg_t;
1289 typedef _Unwind_Action phases_arg_t;
1290 #endif
1292 PERSONALITY_STORAGE _Unwind_Reason_Code
1293 PERSONALITY_FUNCTION (version_arg_t, phases_arg_t,
1294 _Unwind_Exception_Class, _Unwind_Exception *,
1295 _Unwind_Context *);
1297 PERSONALITY_STORAGE _Unwind_Reason_Code
1298 PERSONALITY_FUNCTION (version_arg_t version_arg,
1299 phases_arg_t phases_arg,
1300 _Unwind_Exception_Class uw_exception_class
1301 ATTRIBUTE_UNUSED,
1302 _Unwind_Exception *uw_exception,
1303 _Unwind_Context *uw_context)
1305 /* Fetch the version and phases args with their nominal ABI types for later
1306 use. This is a noop everywhere except on ia64-vms when called from the
1307 Condition Handling Facility. */
1308 int uw_version = (int) version_arg;
1309 _Unwind_Action uw_phases = (_Unwind_Action) phases_arg;
1311 /* Check that we're called from the ABI context we expect, with a major
1312 possible variation on VMS for IA64. */
1313 if (uw_version != 1)
1315 #if defined (VMS) && defined (__IA64)
1317 /* Assume we're called with sigargs/mechargs arguments if really
1318 unexpected bits are set in our first two formals. Redirect to the
1319 GNAT condition handling code in this case. */
1321 extern long __gnat_handle_vms_condition (void *, void *);
1323 unsigned int version_unexpected_bits_mask = 0xffffff00U;
1324 unsigned int phases_unexpected_bits_mask = 0xffffff00U;
1326 if ((unsigned int)uw_version & version_unexpected_bits_mask
1327 && (unsigned int)uw_phases & phases_unexpected_bits_mask)
1328 return __gnat_handle_vms_condition (version_arg, phases_arg);
1329 #endif
1331 return _URC_FATAL_PHASE1_ERROR;
1334 return personality_body (uw_phases, uw_exception, uw_context);
1337 #else /* __ARM_EABI_UNWINDER__ */
1339 PERSONALITY_STORAGE _Unwind_Reason_Code
1340 PERSONALITY_FUNCTION (_Unwind_State state,
1341 struct _Unwind_Exception* ue_header,
1342 struct _Unwind_Context* uw_context);
1344 PERSONALITY_STORAGE _Unwind_Reason_Code
1345 PERSONALITY_FUNCTION (_Unwind_State state,
1346 struct _Unwind_Exception* uw_exception,
1347 struct _Unwind_Context* uw_context)
1349 _Unwind_Action uw_phases;
1351 switch (state & _US_ACTION_MASK)
1353 case _US_VIRTUAL_UNWIND_FRAME:
1354 /* Phase 1. */
1355 uw_phases = _UA_SEARCH_PHASE;
1356 break;
1358 case _US_UNWIND_FRAME_STARTING:
1359 /* Phase 2, to call a cleanup. */
1360 uw_phases = _UA_CLEANUP_PHASE;
1361 #if 0
1362 /* ??? We don't use UA_HANDLER_FRAME (except to debug). Futhermore,
1363 barrier_cache.sp isn't yet set. */
1364 if (!(state & _US_FORCE_UNWIND)
1365 && (uw_exception->barrier_cache.sp
1366 == _Unwind_GetGR (uw_context, UNWIND_STACK_REG)))
1367 uw_phases |= _UA_HANDLER_FRAME;
1368 #endif
1369 break;
1371 case _US_UNWIND_FRAME_RESUME:
1372 /* Phase 2, called at the return of a cleanup. In the GNU
1373 implementation, there is nothing left to do, so we simply go on. */
1374 return continue_unwind (uw_exception, uw_context);
1376 default:
1377 return _URC_FAILURE;
1379 uw_phases |= (state & _US_FORCE_UNWIND);
1381 /* The dwarf unwinder assumes the context structure holds things like the
1382 function and LSDA pointers. The ARM implementation caches these in
1383 the exception header (UCB). To avoid rewriting everything we make a
1384 virtual scratch register point at the UCB. This is a GNU specific
1385 requirement. */
1386 _Unwind_SetGR (uw_context, UNWIND_POINTER_REG, (_Unwind_Ptr) uw_exception);
1388 return personality_body (uw_phases, uw_exception, uw_context);
1390 #endif /* __ARM_EABI_UNWINDER__ */
1392 /* Callback routine called by Unwind_ForcedUnwind to execute all the cleanup
1393 before exiting the task. */
1395 #ifndef CERT
1396 _Unwind_Reason_Code
1397 __gnat_cleanupunwind_handler (int version ATTRIBUTE_UNUSED,
1398 _Unwind_Action phases,
1399 _Unwind_Exception_Class eclass ATTRIBUTE_UNUSED,
1400 struct _Unwind_Exception *exception,
1401 struct _Unwind_Context *context ATTRIBUTE_UNUSED,
1402 void *arg ATTRIBUTE_UNUSED)
1404 /* Terminate when the end of the stack is reached. */
1405 if ((phases & _UA_END_OF_STACK) != 0
1406 #if defined (__ia64__) && defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
1407 /* Strictely follow the ia64 ABI: when end of stack is reached,
1408 the callback will be called with a NULL stack pointer.
1409 No need for that when using libgcc unwinder. */
1410 || _Unwind_GetGR (context, 12) == 0
1411 #endif
1413 __gnat_unhandled_except_handler (exception);
1415 /* We know there is at least one cleanup further up. Return so that it
1416 is searched and entered, after which Unwind_Resume will be called
1417 and this hook will gain control again. */
1418 return _URC_NO_REASON;
1420 #endif
1422 /* Define the consistently named wrappers imported by Propagate_Exception. */
1424 _Unwind_Reason_Code
1425 __gnat_Unwind_RaiseException (_Unwind_Exception *e)
1427 #ifdef __USING_SJLJ_EXCEPTIONS__
1428 return _Unwind_SjLj_RaiseException (e);
1429 #else
1430 return _Unwind_RaiseException (e);
1431 #endif
1434 _Unwind_Reason_Code
1435 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *e ATTRIBUTE_UNUSED,
1436 _Unwind_Stop_Fn handler ATTRIBUTE_UNUSED,
1437 void *argument ATTRIBUTE_UNUSED)
1439 #ifdef __USING_SJLJ_EXCEPTIONS__
1441 # if defined (__APPLE__) && defined (__arm__)
1442 /* There is not ForcedUnwind routine in arm-darwin system library. */
1443 return _URC_FATAL_PHASE1_ERROR;
1444 # else
1445 return _Unwind_SjLj_ForcedUnwind (e, handler, argument);
1446 # endif
1448 #else
1449 return _Unwind_ForcedUnwind (e, handler, argument);
1450 #endif
1453 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
1455 #define STATUS_USER_DEFINED (1U << 29)
1457 /* From unwind-seh.c. */
1458 #define GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C')
1459 #define GCC_EXCEPTION(TYPE) \
1460 (STATUS_USER_DEFINED | ((TYPE) << 24) | GCC_MAGIC)
1461 #define STATUS_GCC_THROW GCC_EXCEPTION (0)
1463 struct Exception_Data *
1464 __gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg);
1466 struct _Unwind_Exception *
1467 __gnat_create_machine_occurrence_from_signal_handler (Exception_Id,
1468 const char *);
1470 /* Unwind opcodes. */
1471 #define UWOP_PUSH_NONVOL 0
1472 #define UWOP_ALLOC_LARGE 1
1473 #define UWOP_ALLOC_SMALL 2
1474 #define UWOP_SET_FPREG 3
1475 #define UWOP_SAVE_NONVOL 4
1476 #define UWOP_SAVE_NONVOL_FAR 5
1477 #define UWOP_SAVE_XMM128 8
1478 #define UWOP_SAVE_XMM128_FAR 9
1479 #define UWOP_PUSH_MACHFRAME 10
1481 /* Modify the IP value saved in the machine frame. This is really a kludge,
1482 that will be removed if we could propagate the Windows exception (and not
1483 the GCC one).
1485 What is very wrong is that the Windows unwinder will try to decode the
1486 instruction at IP, which isn't valid anymore after the adjustment. */
1488 static void
1489 __gnat_adjust_context (unsigned char *unw, ULONG64 rsp)
1491 unsigned int len;
1493 /* Version 1 or 2. */
1494 if (unw[0] != 1 && unw[0] != 2)
1495 return;
1496 /* No flags, no prologue. */
1497 if (unw[1] != 0)
1498 return;
1499 len = unw[2];
1500 /* No frame. */
1501 if (unw[3] != 0)
1502 return;
1503 /* ??? Skip the first 2 undocumented opcodes for version 2. */
1504 if (unw[0] == 2)
1505 unw += 8;
1506 else
1507 unw += 4;
1508 while (len > 0)
1510 /* Offset in prologue = 0. */
1511 if (unw[0] != 0)
1512 return;
1513 switch (unw[1] & 0xf)
1515 case UWOP_ALLOC_LARGE:
1516 /* Expect < 512KB. */
1517 if ((unw[1] & 0xf0) != 0)
1518 return;
1519 rsp += *(unsigned short *)(unw + 2) * 8;
1520 len--;
1521 unw += 2;
1522 break;
1523 case UWOP_SAVE_NONVOL:
1524 case UWOP_SAVE_XMM128:
1525 len--;
1526 unw += 2;
1527 break;
1528 case UWOP_PUSH_MACHFRAME:
1530 ULONG64 *rip;
1531 rip = (ULONG64 *)rsp;
1532 if ((unw[1] & 0xf0) == 0x10)
1533 rip++;
1534 /* Adjust rip. */
1535 (*rip)++;
1537 return;
1538 default:
1539 /* Unexpected. */
1540 return;
1542 unw += 2;
1543 len--;
1547 EXCEPTION_DISPOSITION
1548 __gnat_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
1549 PCONTEXT ms_orig_context,
1550 PDISPATCHER_CONTEXT ms_disp)
1552 /* Possibly transform run-time errors into Ada exceptions. */
1553 if (!(ms_exc->ExceptionCode & STATUS_USER_DEFINED))
1555 struct Exception_Data *exception;
1556 const char *msg;
1557 ULONG64 excpip = (ULONG64) ms_exc->ExceptionAddress;
1559 if (excpip != 0
1560 && excpip >= (ms_disp->ImageBase
1561 + ms_disp->FunctionEntry->BeginAddress)
1562 && excpip < (ms_disp->ImageBase
1563 + ms_disp->FunctionEntry->EndAddress))
1565 /* This is a fault in this function. We need to adjust the return
1566 address before raising the GCC exception. In order to do that,
1567 we need to locate the machine frame that has been pushed onto
1568 the stack in response to the hardware exception, so we will do
1569 a private unwinding from here, i.e. the frame of the personality
1570 routine, up to the frame immediately following the frame of this
1571 function. This frame corresponds to a dummy prologue which is
1572 never actually executed but instead appears before the real entry
1573 point of an interrupt routine and exists only to provide a place
1574 to simulate the push of a machine frame. */
1575 CONTEXT context;
1576 PRUNTIME_FUNCTION mf_func = NULL;
1577 ULONG64 mf_imagebase;
1578 ULONG64 mf_rsp = 0;
1580 /* Get the current context. */
1581 RtlCaptureContext (&context);
1583 while (1)
1585 PRUNTIME_FUNCTION RuntimeFunction;
1586 ULONG64 ImageBase;
1587 VOID *HandlerData;
1588 ULONG64 EstablisherFrame;
1590 /* Get function metadata. */
1591 RuntimeFunction
1592 = RtlLookupFunctionEntry (context.Rip, &ImageBase,
1593 ms_disp->HistoryTable);
1595 /* Stop once we reached the frame of this function. */
1596 if (RuntimeFunction == ms_disp->FunctionEntry)
1597 break;
1599 mf_func = RuntimeFunction;
1600 mf_imagebase = ImageBase;
1601 mf_rsp = context.Rsp;
1603 if (RuntimeFunction)
1605 /* Unwind. */
1606 RtlVirtualUnwind (0, ImageBase, context.Rip, RuntimeFunction,
1607 &context, &HandlerData, &EstablisherFrame,
1608 NULL);
1610 else
1612 /* In case of failure, assume this is a leaf function. */
1613 context.Rip = *(ULONG64 *) context.Rsp;
1614 context.Rsp += 8;
1617 /* 0 means bottom of the stack. */
1618 if (context.Rip == 0)
1620 mf_func = NULL;
1621 break;
1625 /* If we have found the machine frame, adjust the return address. */
1626 if (mf_func != NULL)
1627 __gnat_adjust_context
1628 ((unsigned char *)(mf_imagebase + mf_func->UnwindData), mf_rsp);
1631 exception = __gnat_map_SEH (ms_exc, &msg);
1632 if (exception != NULL)
1634 /* Directly convert the system exception into a GCC one.
1636 This is really breaking the API, but is necessary for stack size
1637 reasons: the normal way is to call Raise_From_Signal_Handler,
1638 which builds the exception and calls _Unwind_RaiseException,
1639 which unwinds the stack and will call this personality routine.
1640 But the Windows unwinder needs about 2KB of stack. */
1641 struct _Unwind_Exception *exc
1642 = __gnat_create_machine_occurrence_from_signal_handler (exception,
1643 msg);
1644 memset (exc->private_, 0, sizeof (exc->private_));
1645 ms_exc->ExceptionCode = STATUS_GCC_THROW;
1646 ms_exc->NumberParameters = 1;
1647 ms_exc->ExceptionInformation[0] = (ULONG_PTR)exc;
1652 return
1653 _GCC_specific_handler (ms_exc, this_frame, ms_orig_context, ms_disp,
1654 __gnat_personality_imp);
1657 #endif /* SEH */
1659 #if !defined (__USING_SJLJ_EXCEPTIONS__)
1660 /* Size of the _Unwind_Exception structure. This is used by g-cppexc to get
1661 the offset to the C++ object. */
1663 const int __gnat_unwind_exception_size = sizeof (_Unwind_Exception);
1664 #endif
1666 #ifdef __cplusplus
1668 #endif