1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2011, Free Software Foundation, Inc. *
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. *
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. *
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/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
32 /* Code related to the integration of the GCC mechanism for exception
38 /* In the top-of-tree GCC, tconfig does not include tm.h, but in GCC 3.2
39 it does. To avoid branching raise.c just for that purpose, we kludge by
40 looking for a symbol always defined by tm.h and if it's not defined,
42 #ifndef FIRST_PSEUDO_REGISTER
43 #include "coretypes.h"
60 /* On MacOS X, versions older than 10.5 don't export _Unwind_GetIPInfo. */
62 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
63 #define HAVE_GETIPINFO 1
67 /* The names of a couple of "standard" routines for unwinding/propagation
68 actually vary depending on the underlying GCC scheme for exception handling
69 (SJLJ or DWARF). We need a consistently named interface to import from
70 a-except, so wrappers are defined here.
72 Besides, even though the compiler is never setup to use the GCC propagation
73 circuitry, it still relies on exceptions internally and part of the sources
74 to handle to exceptions are shared with the run-time library. We need
75 dummy definitions for the wrappers to satisfy the linker in this case.
77 The types to be used by those wrappers in the run-time library are target
78 types exported by unwind.h. We used to piggyback on them for the compiler
79 stubs, but there is no guarantee that unwind.h is always in sight so we
80 define our own set below. These are dummy types as the wrappers are never
81 called in the compiler case. */
87 typedef struct _Unwind_Context _Unwind_Context;
88 typedef struct _Unwind_Exception _Unwind_Exception;
92 typedef void _Unwind_Context;
93 typedef void _Unwind_Exception;
94 typedef int _Unwind_Reason_Code;
99 __gnat_Unwind_RaiseException (_Unwind_Exception *);
102 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *, void *, void *);
104 extern void __gnat_setup_current_excep (_Unwind_Exception *);
106 #ifdef IN_RTS /* For eh personality routine */
109 #include "unwind-dw2-fde.h"
110 #include "unwind-pe.h"
112 /* The known and handled exception classes. */
114 #define CXX_EXCEPTION_CLASS 0x474e5543432b2b00ULL
115 #define GNAT_EXCEPTION_CLASS 0x474e552d41646100ULL
117 /* --------------------------------------------------------------
118 -- The DB stuff below is there for debugging purposes only. --
119 -------------------------------------------------------------- */
121 #define DB_PHASES 0x1
123 #define DB_ACTIONS 0x4
124 #define DB_REGIONS 0x8
126 #define DB_ERR 0x1000
128 /* The "action" stuff below is also there for debugging purposes only. */
132 _Unwind_Action phase;
136 static const phase_descriptor phase_descriptors[]
137 = {{ _UA_SEARCH_PHASE, "SEARCH_PHASE" },
138 { _UA_CLEANUP_PHASE, "CLEANUP_PHASE" },
139 { _UA_HANDLER_FRAME, "HANDLER_FRAME" },
140 { _UA_FORCE_UNWIND, "FORCE_UNWIND" },
144 db_accepted_codes (void)
146 static int accepted_codes = -1;
148 if (accepted_codes == -1)
150 char * db_env = (char *) getenv ("EH_DEBUG");
152 accepted_codes = db_env ? (atoi (db_env) | DB_ERR) : 0;
153 /* Arranged for ERR stuff to always be visible when the variable
154 is defined. One may just set the variable to 0 to see the ERR
158 return accepted_codes;
161 #define DB_INDENT_INCREASE 0x01
162 #define DB_INDENT_DECREASE 0x02
163 #define DB_INDENT_OUTPUT 0x04
164 #define DB_INDENT_NEWLINE 0x08
165 #define DB_INDENT_RESET 0x10
167 #define DB_INDENT_UNIT 8
170 db_indent (int requests)
172 static int current_indentation_level = 0;
174 if (requests & DB_INDENT_RESET)
176 current_indentation_level = 0;
179 if (requests & DB_INDENT_INCREASE)
181 current_indentation_level ++;
184 if (requests & DB_INDENT_DECREASE)
186 current_indentation_level --;
189 if (requests & DB_INDENT_NEWLINE)
191 fprintf (stderr, "\n");
194 if (requests & DB_INDENT_OUTPUT)
196 fprintf (stderr, "%*s",
197 current_indentation_level * DB_INDENT_UNIT, " ");
202 static void ATTRIBUTE_PRINTF_2
203 db (int db_code, char * msg_format, ...)
205 if (db_accepted_codes () & db_code)
209 db_indent (DB_INDENT_OUTPUT);
211 va_start (msg_args, msg_format);
212 vfprintf (stderr, msg_format, msg_args);
218 db_phases (int phases)
220 phase_descriptor *a = phase_descriptors;
222 if (! (db_accepted_codes() & DB_PHASES))
225 db (DB_PHASES, "\n");
227 for (; a->description != 0; a++)
228 if (phases & a->phase)
229 db (DB_PHASES, "%s ", a->description);
231 db (DB_PHASES, " :\n");
235 /* ---------------------------------------------------------------
236 -- Now come a set of useful structures and helper routines. --
237 --------------------------------------------------------------- */
239 /* There are three major runtime tables involved, generated by the
240 GCC back-end. Contents slightly vary depending on the underlying
241 implementation scheme (dwarf zero cost / sjlj).
243 =======================================
244 * Tables for the dwarf zero cost case *
245 =======================================
248 -------------------------------------------------------------------
249 * region-start | region-length | landing-pad | first-action-index *
250 -------------------------------------------------------------------
252 Identify possible actions to be taken and where to resume control
253 for that when an exception propagates through a pc inside the region
254 delimited by start and length.
256 A null landing-pad indicates that nothing is to be done.
258 Otherwise, first-action-index provides an entry into the action[]
259 table which heads a list of possible actions to be taken (see below).
261 If it is determined that indeed an action should be taken, that
262 is, if one action filter matches the exception being propagated,
263 then control should be transfered to landing-pad.
265 A null first-action-index indicates that there are only cleanups
269 -------------------------------
270 * action-filter | next-action *
271 -------------------------------
273 This table contains lists (called action chains) of possible actions
274 associated with call-site entries described in the call-site [] table.
275 There is at most one action list per call-site entry.
277 A null action-filter indicates a cleanup.
279 Non null action-filters provide an index into the ttypes [] table
280 (see below), from which information may be retrieved to check if it
281 matches the exception being propagated.
283 action-filter > 0 means there is a regular handler to be run,
285 action-filter < 0 means there is a some "exception_specification"
286 data to retrieve, which is only relevant for C++
287 and should never show up for Ada.
289 next-action indexes the next entry in the list. 0 indicates there is
297 A null value indicates a catch-all handler in C++, and an "others"
300 Non null values are used to match the exception being propagated:
301 In C++ this is a pointer to some rtti data, while in Ada this is an
304 The special id value 1 indicates an "all_others" handler.
306 For C++, this table is actually also used to store "exception
307 specification" data. The differentiation between the two kinds
308 of entries is made by the sign of the associated action filter,
309 which translates into positive or negative offsets from the
310 so called base of the table:
312 Exception Specification data is stored at positive offsets from
313 the ttypes table base, which Exception Type data is stored at
316 ---------------------------------------------------------------------------
318 Here is a quick summary of the tables organization:
320 +-- Unwind_Context (pc, ...)
326 | +=============================================================+
327 | | region-start + length | landing-pad | first-action-index |
328 | +=============================================================+
329 +-> | pc range 0 => no-action 0 => cleanups only |
330 | !0 => jump @ N --+ |
331 +====================================================== | ====+
336 +==========================================================+ |
337 | action-filter | next-action | |
338 +==========================================================+ |
340 | >0 => ttype index for handler ------+ 0 => end of chain | <-+
341 | <0 => ttype index for spec data | |
342 +==================================== | ===================+
346 | Offset negated from
347 +=====================+ | the actual base.
349 +============+=====================+ |
350 | | 0 => "others" | |
351 | ... | 1 => "all others" | <---+
352 | | X => exception id |
353 | handlers +---------------------+
357 +============+=====================+ <<------ Table base
359 | specs | ... | (should not see negative filter
360 | ... | ... | values for Ada).
361 +============+=====================+
364 ============================
365 * Tables for the sjlj case *
366 ============================
368 So called "function contexts" are pushed on a context stack by calls to
369 _Unwind_SjLj_Register on function entry, and popped off at exit points by
370 calls to _Unwind_SjLj_Unregister. The current call_site for a function is
371 updated in the function context as the function's code runs along.
373 The generic unwinding engine in _Unwind_RaiseException walks the function
374 context stack and not the actual call chain.
376 The ACTION and TTYPES tables remain unchanged, which allows to search them
377 during the propagation phase to determine whether or not the propagated
378 exception is handled somewhere. When it is, we only "jump" up once directly
379 to the context where the handler will be found. Besides, this allows "break
380 exception unhandled" to work also
382 The CALL-SITE table is setup differently, though: the pc attached to the
383 unwind context is a direct index into the table, so the entries in this
384 table do not hold region bounds any more.
386 A special index (-1) is used to indicate that no action is possibly
387 connected with the context at hand, so null landing pads cannot appear
390 Additionally, landing pad values in the table do not represent code address
391 to jump at, but so called "dispatch" indices used by a common landing pad
392 for the function to switch to the appropriate post-landing-pad.
394 +-- Unwind_Context (pc, ...)
396 | pc = call-site index
397 | 0 => terminate (should not see this for Ada)
402 | +=====================================+
403 | | landing-pad | first-action-index |
404 | +=====================================+
405 +-> | 0 => cleanups only |
407 +=====================================+
410 ===================================
411 * Basic organization of this unit *
412 ===================================
414 The major point of this unit is to provide an exception propagation
415 personality routine for Ada. This is __gnat_personality_v0.
417 It is provided with a pointer to the propagated exception, an unwind
418 context describing a location the propagation is going through, and a
419 couple of other arguments including a description of the current
422 It shall return to the generic propagation engine what is to be performed
423 next, after possible context adjustments, depending on what it finds in the
424 traversed context (a handler for the exception, a cleanup, nothing, ...),
425 and on the propagation phase.
427 A number of structures and subroutines are used for this purpose, as
430 o region_descriptor: General data associated with the context (base pc,
431 call-site table, action table, ttypes table, ...)
433 o action_descriptor: Data describing the action to be taken for the
434 propagated exception in the provided context (kind of action: nothing,
435 handler, cleanup; pointer to the action table entry, ...).
441 Propagate_Exception (a-exexpr.adb)
444 _Unwind_RaiseException (libgcc)
448 +--> __gnat_personality_v0 (context, exception)
450 +--> get_region_descriptor_for (context)
452 +--> get_action_descriptor_for (context, exception, region)
454 | +--> get_call_site_action_for (context, region)
455 | (one version for each underlying scheme)
457 +--> setup_to_install (context)
459 This unit is inspired from the C++ version found in eh_personality.cc,
460 part of libstdc++-v3.
465 /* This is an incomplete "proxy" of the structure of exception objects as
466 built by the GNAT runtime library. Accesses to other fields than the common
467 header are performed through subprogram calls to alleviate the need of an
468 exact counterpart here and potential alignment/size issues for the common
469 header. See a-exexpr.adb. */
473 _Unwind_Exception common;
474 /* ABI header, maximally aligned. */
477 /* The two constants below are specific ttype identifiers for special
478 exception ids. Their type should match what a-exexpr exports. */
480 extern const int __gnat_others_value;
481 #define GNAT_OTHERS ((_Unwind_Ptr) &__gnat_others_value)
483 extern const int __gnat_all_others_value;
484 #define GNAT_ALL_OTHERS ((_Unwind_Ptr) &__gnat_all_others_value)
486 /* Describe the useful region data associated with an unwind context. */
490 /* The base pc of the region. */
493 /* Pointer to the Language Specific Data for the region. */
496 /* Call-Site data associated with this region. */
497 unsigned char call_site_encoding;
498 const unsigned char *call_site_table;
500 /* The base to which are relative landing pad offsets inside the call-site
504 /* Action-Table associated with this region. */
505 const unsigned char *action_table;
507 /* Ttype data associated with this region. */
508 unsigned char ttype_encoding;
509 const unsigned char *ttype_table;
510 _Unwind_Ptr ttype_base;
515 db_region_for (region_descriptor *region, _Unwind_Context *uw_context)
517 int ip_before_insn = 0;
518 #ifdef HAVE_GETIPINFO
519 _Unwind_Ptr ip = _Unwind_GetIPInfo (uw_context, &ip_before_insn);
521 _Unwind_Ptr ip = _Unwind_GetIP (uw_context);
526 if (! (db_accepted_codes () & DB_REGIONS))
529 db (DB_REGIONS, "For ip @ 0x%08x => ", ip);
532 db (DB_REGIONS, "lsda @ 0x%x", region->lsda);
534 db (DB_REGIONS, "no lsda");
536 db (DB_REGIONS, "\n");
539 /* Retrieve the ttype entry associated with FILTER in the REGION's
542 static const _Unwind_Ptr
543 get_ttype_entry_for (region_descriptor *region, long filter)
545 _Unwind_Ptr ttype_entry;
547 filter *= size_of_encoded_value (region->ttype_encoding);
548 read_encoded_value_with_base
549 (region->ttype_encoding, region->ttype_base,
550 region->ttype_table - filter, &ttype_entry);
555 /* Fill out the REGION descriptor for the provided UW_CONTEXT. */
558 get_region_description_for (_Unwind_Context *uw_context,
559 region_descriptor *region)
561 const unsigned char * p;
563 unsigned char lpbase_encoding;
565 /* Get the base address of the lsda information. If the provided context
566 is null or if there is no associated language specific data, there's
567 nothing we can/should do. */
569 = (_Unwind_Ptr) (uw_context
570 ? _Unwind_GetLanguageSpecificData (uw_context) : 0);
575 /* Parse the lsda and fill the region descriptor. */
576 p = (char *)region->lsda;
578 region->base = _Unwind_GetRegionStart (uw_context);
580 /* Find @LPStart, the base to which landing pad offsets are relative. */
581 lpbase_encoding = *p++;
582 if (lpbase_encoding != DW_EH_PE_omit)
583 p = read_encoded_value
584 (uw_context, lpbase_encoding, p, ®ion->lp_base);
586 region->lp_base = region->base;
588 /* Find @TType, the base of the handler and exception spec type data. */
589 region->ttype_encoding = *p++;
590 if (region->ttype_encoding != DW_EH_PE_omit)
592 p = read_uleb128 (p, &tmp);
593 region->ttype_table = p + tmp;
596 region->ttype_table = 0;
599 = base_of_encoded_value (region->ttype_encoding, uw_context);
601 /* Get the encoding and length of the call-site table; the action table
602 immediately follows. */
603 region->call_site_encoding = *p++;
604 region->call_site_table = read_uleb128 (p, &tmp);
606 region->action_table = region->call_site_table + tmp;
610 /* Describe an action to be taken when propagating an exception up to
615 /* Found some call site base data, but need to analyze further
616 before being able to decide. */
619 /* There is nothing relevant in the context at hand. */
622 /* There are only cleanups to run in this context. */
625 /* There is a handler for the exception in this context. */
629 /* filter value for cleanup actions. */
630 static const int cleanup_filter = 0;
634 /* The kind of action to be taken. */
637 /* A pointer to the action record entry. */
638 const unsigned char *table_entry;
640 /* Where we should jump to actually take an action (trigger a cleanup or an
641 exception handler). */
642 _Unwind_Ptr landing_pad;
644 /* If we have a handler matching our exception, these are the filter to
645 trigger it and the corresponding id. */
646 _Unwind_Sword ttype_filter;
647 _Unwind_Ptr ttype_entry;
652 db_action_for (action_descriptor *action, _Unwind_Context *uw_context)
654 int ip_before_insn = 0;
655 #ifdef HAVE_GETIPINFO
656 _Unwind_Ptr ip = _Unwind_GetIPInfo (uw_context, &ip_before_insn);
658 _Unwind_Ptr ip = _Unwind_GetIP (uw_context);
663 db (DB_ACTIONS, "For ip @ 0x%08x => ", ip);
665 switch (action->kind)
668 db (DB_ACTIONS, "lpad @ 0x%x, record @ 0x%x\n",
669 action->landing_pad, action->table_entry);
673 db (DB_ACTIONS, "Nothing\n");
677 db (DB_ACTIONS, "Cleanup\n");
681 db (DB_ACTIONS, "Handler, filter = %d\n", action->ttype_filter);
685 db (DB_ACTIONS, "Err? Unexpected action kind !\n");
692 /* Search the call_site_table of REGION for an entry appropriate for the
693 UW_CONTEXT's IP. If one is found, store the associated landing_pad
694 and action_table entry, and set the ACTION kind to unknown for further
695 analysis. Otherwise, set the ACTION kind to nothing.
697 There are two variants of this routine, depending on the underlying
698 mechanism (DWARF/SJLJ), which account for differences in the tables. */
700 #ifdef __USING_SJLJ_EXCEPTIONS__
702 #define __builtin_eh_return_data_regno(x) x
705 get_call_site_action_for (_Unwind_Context *uw_context,
706 region_descriptor *region,
707 action_descriptor *action)
709 int ip_before_insn = 0;
710 #ifdef HAVE_GETIPINFO
711 _Unwind_Ptr call_site = _Unwind_GetIPInfo (uw_context, &ip_before_insn);
713 _Unwind_Ptr call_site = _Unwind_GetIP (uw_context);
715 /* Subtract 1 if necessary because GetIPInfo returns the actual call site
716 value + 1 in this case. */
720 /* call_site is a direct index into the call-site table, with two special
721 values : -1 for no-action and 0 for "terminate". The latter should never
722 show up for Ada. To test for the former, beware that _Unwind_Ptr might
725 if ((int)call_site < 0)
727 action->kind = nothing;
730 else if (call_site == 0)
732 db (DB_ERR, "========> Err, null call_site for Ada/sjlj\n");
733 action->kind = nothing;
738 _uleb128_t cs_lp, cs_action;
740 /* Let the caller know there may be an action to take, but let it
741 determine the kind. */
742 action->kind = unknown;
744 /* We have a direct index into the call-site table, but this table is
745 made of leb128 values, the encoding length of which is variable. We
746 can't merely compute an offset from the index, then, but have to read
747 all the entries before the one of interest. */
749 const unsigned char *p = region->call_site_table;
752 p = read_uleb128 (p, &cs_lp);
753 p = read_uleb128 (p, &cs_action);
754 } while (--call_site);
756 action->landing_pad = cs_lp + 1;
759 action->table_entry = region->action_table + cs_action - 1;
761 action->table_entry = 0;
767 #else /* !__USING_SJLJ_EXCEPTIONS__ */
770 get_call_site_action_for (_Unwind_Context *uw_context,
771 region_descriptor *region,
772 action_descriptor *action)
774 const unsigned char *p = region->call_site_table;
775 int ip_before_insn = 0;
776 #ifdef HAVE_GETIPINFO
777 _Unwind_Ptr ip = _Unwind_GetIPInfo (uw_context, &ip_before_insn);
779 _Unwind_Ptr ip = _Unwind_GetIP (uw_context);
781 /* Subtract 1 if necessary because GetIPInfo yields a call return address
782 in this case, while we are interested in information for the call point.
783 This does not always yield the exact call instruction address but always
784 brings the IP back within the corresponding region. */
788 /* Unless we are able to determine otherwise... */
789 action->kind = nothing;
793 while (p < region->action_table)
795 _Unwind_Ptr cs_start, cs_len, cs_lp;
796 _uleb128_t cs_action;
798 /* Note that all call-site encodings are "absolute" displacements. */
799 p = read_encoded_value (0, region->call_site_encoding, p, &cs_start);
800 p = read_encoded_value (0, region->call_site_encoding, p, &cs_len);
801 p = read_encoded_value (0, region->call_site_encoding, p, &cs_lp);
802 p = read_uleb128 (p, &cs_action);
805 "c_site @ 0x%08x (+0x%03x), len = %3d, lpad @ 0x%08x (+0x%03x)\n",
806 region->base+cs_start, cs_start, cs_len,
807 region->lp_base+cs_lp, cs_lp);
809 /* The table is sorted, so if we've passed the IP, stop. */
810 if (ip < region->base + cs_start)
813 /* If we have a match, fill the ACTION fields accordingly. */
814 else if (ip < region->base + cs_start + cs_len)
816 /* Let the caller know there may be an action to take, but let it
817 determine the kind. */
818 action->kind = unknown;
821 action->landing_pad = region->lp_base + cs_lp;
823 action->landing_pad = 0;
826 action->table_entry = region->action_table + cs_action - 1;
828 action->table_entry = 0;
830 db (DB_CSITE, "+++\n");
835 db (DB_CSITE, "---\n");
838 #endif /* __USING_SJLJ_EXCEPTIONS__ */
840 /* With CHOICE an exception choice representing an "exception - when"
841 argument, and PROPAGATED_EXCEPTION a pointer to the currently propagated
842 occurrence, return true if the latter matches the former, that is, if
843 PROPAGATED_EXCEPTION is caught by the handling code controlled by CHOICE.
844 This takes care of the special Non_Ada_Error case on VMS. */
846 #define Is_Handled_By_Others __gnat_is_handled_by_others
847 #define Language_For __gnat_language_for
848 #define Import_Code_For __gnat_import_code_for
849 #define EID_For __gnat_eid_for
851 extern bool Is_Handled_By_Others (_Unwind_Ptr eid);
852 extern char Language_For (_Unwind_Ptr eid);
854 extern Exception_Code Import_Code_For (_Unwind_Ptr eid);
856 extern Exception_Id EID_For (_GNAT_Exception * e);
859 is_handled_by (_Unwind_Ptr choice, _GNAT_Exception * propagated_exception)
861 if (propagated_exception->common.exception_class == GNAT_EXCEPTION_CLASS)
863 /* Pointer to the GNAT exception data corresponding to the propagated
865 _Unwind_Ptr E = (_Unwind_Ptr) EID_For (propagated_exception);
867 /* Base matching rules: An exception data (id) matches itself, "when
868 all_others" matches anything and "when others" matches anything
869 unless explicitly stated otherwise in the propagated occurrence. */
873 || choice == GNAT_ALL_OTHERS
874 || (choice == GNAT_OTHERS && Is_Handled_By_Others (E));
876 /* In addition, on OpenVMS, Non_Ada_Error matches VMS exceptions, and we
877 may have different exception data pointers that should match for the
878 same condition code, if both an export and an import have been
879 registered. The import code for both the choice and the propagated
880 occurrence are expected to have been masked off regarding severity
881 bits already (at registration time for the former and from within the
882 low level exception vector for the latter). */
884 # define Non_Ada_Error system__aux_dec__non_ada_error
885 extern struct Exception_Data Non_Ada_Error;
888 (Language_For (E) == 'V'
889 && choice != GNAT_OTHERS && choice != GNAT_ALL_OTHERS
890 && ((Language_For (choice) == 'V' && Import_Code_For (choice) != 0
891 && Import_Code_For (choice) == Import_Code_For (E))
892 || choice == (_Unwind_Ptr)&Non_Ada_Error));
899 # define Foreign_Exception system__exceptions__foreign_exception;
900 extern struct Exception_Data Foreign_Exception;
902 return choice == GNAT_ALL_OTHERS
903 || choice == GNAT_OTHERS
904 || choice == (_Unwind_Ptr)&Foreign_Exception;
908 /* Fill out the ACTION to be taken from propagating UW_EXCEPTION up to
909 UW_CONTEXT in REGION. */
912 get_action_description_for (_Unwind_Context *uw_context,
913 _Unwind_Exception *uw_exception,
914 region_descriptor *region,
915 action_descriptor *action)
917 _GNAT_Exception * gnat_exception = (_GNAT_Exception *) uw_exception;
919 /* Search the call site table first, which may get us a landing pad as well
920 as the head of an action record list. */
921 get_call_site_action_for (uw_context, region, action);
922 db_action_for (action, uw_context);
924 /* If there is not even a call_site entry, we are done. */
925 if (action->kind == nothing)
928 /* Otherwise, check what we have at the place of the call site. */
930 /* No landing pad => no cleanups or handlers. */
931 if (action->landing_pad == 0)
933 action->kind = nothing;
937 /* Landing pad + null table entry => only cleanups. */
938 else if (action->table_entry == 0)
940 action->kind = cleanup;
941 action->ttype_filter = cleanup_filter;
942 /* The filter initialization is not strictly necessary, as cleanup-only
943 landing pads don't look at the filter value. It is there to ensure
944 we don't pass random values and so trigger potential confusion when
945 installing the context later on. */
949 /* Landing pad + Table entry => handlers + possible cleanups. */
952 const unsigned char * p = action->table_entry;
954 _sleb128_t ar_filter, ar_disp;
956 action->kind = nothing;
960 p = read_sleb128 (p, &ar_filter);
961 read_sleb128 (p, &ar_disp);
962 /* Don't assign p here, as it will be incremented by ar_disp
965 /* Null filters are for cleanups. */
966 if (ar_filter == cleanup_filter)
968 action->kind = cleanup;
969 action->ttype_filter = cleanup_filter;
970 /* The filter initialization is required here, to ensure
971 the target landing pad branches to the cleanup code if
972 we happen not to find a matching handler. */
975 /* Positive filters are for regular handlers. */
976 else if (ar_filter > 0)
978 /* See if the filter we have is for an exception which matches
979 the one we are propagating. */
980 _Unwind_Ptr choice = get_ttype_entry_for (region, ar_filter);
982 if (is_handled_by (choice, gnat_exception))
984 action->kind = handler;
985 action->ttype_filter = ar_filter;
986 action->ttype_entry = choice;
991 /* Negative filter values are for C++ exception specifications.
992 Should not be there for Ada :/ */
994 db (DB_ERR, "========> Err, filter < 0 for Ada/dwarf\n");
1004 /* Setup in UW_CONTEXT the eh return target IP and data registers, which will
1005 be restored with the others and retrieved by the landing pad once the jump
1009 setup_to_install (_Unwind_Context *uw_context,
1010 _Unwind_Exception *uw_exception,
1011 _Unwind_Ptr uw_landing_pad,
1014 #ifndef EH_RETURN_DATA_REGNO
1015 /* We should not be called if the appropriate underlying support is not
1019 /* 1/ exception object pointer, which might be provided back to
1020 _Unwind_Resume (and thus to this personality routine) if we are jumping
1022 _Unwind_SetGR (uw_context, __builtin_eh_return_data_regno (0),
1023 (_Unwind_Word)uw_exception);
1025 /* 2/ handler switch value register, which will also be used by the target
1026 landing pad to decide what action it shall take. */
1027 _Unwind_SetGR (uw_context, __builtin_eh_return_data_regno (1),
1028 (_Unwind_Word)uw_filter);
1030 /* Setup the address we should jump at to reach the code where there is the
1031 "something" we found. */
1032 _Unwind_SetIP (uw_context, uw_landing_pad);
1036 /* The following is defined from a-except.adb. Its purpose is to enable
1037 automatic backtraces upon exception raise, as provided through the
1038 GNAT.Traceback facilities. */
1039 extern void __gnat_notify_handled_exception (void);
1040 extern void __gnat_notify_unhandled_exception (void);
1042 /* Below is the eh personality routine per se. We currently assume that only
1043 GNU-Ada exceptions are met. */
1045 #ifdef __USING_SJLJ_EXCEPTIONS__
1046 #define PERSONALITY_FUNCTION __gnat_personality_sj0
1048 #define PERSONALITY_FUNCTION __gnat_personality_v0
1051 /* Major tweak for ia64-vms : the CHF propagation phase calls this personality
1052 routine with sigargs/mechargs arguments and has very specific expectations
1053 on possible return values.
1055 We handle this with a number of specific tricks:
1057 1. We tweak the personality routine prototype to have the "version" and
1058 "phases" two first arguments be void * instead of int and _Unwind_Action
1059 as nominally expected in the GCC context.
1061 This allows us to access the full range of bits passed in every case and
1062 has no impact on the callers side since each argument remains assigned
1063 the same single 64bit slot.
1065 2. We retrieve the corresponding int and _Unwind_Action values within the
1066 routine for regular use with truncating conversions. This is a noop when
1067 called from the libgcc unwinder.
1069 3. We assume we're called by the VMS CHF when unexpected bits are set in
1070 both those values. The incoming arguments are then real sigargs and
1071 mechargs pointers, which we then redirect to __gnat_handle_vms_condition
1072 for proper processing.
1074 #if defined (VMS) && defined (__IA64)
1075 typedef void * version_arg_t;
1076 typedef void * phases_arg_t;
1078 typedef int version_arg_t;
1079 typedef _Unwind_Action phases_arg_t;
1083 PERSONALITY_FUNCTION (version_arg_t, phases_arg_t,
1084 _Unwind_Exception_Class, _Unwind_Exception *,
1088 PERSONALITY_FUNCTION (version_arg_t version_arg,
1089 phases_arg_t phases_arg,
1090 _Unwind_Exception_Class uw_exception_class,
1091 _Unwind_Exception *uw_exception,
1092 _Unwind_Context *uw_context)
1094 /* Fetch the version and phases args with their nominal ABI types for later
1095 use. This is a noop everywhere except on ia64-vms when called from the
1096 Condition Handling Facility. */
1097 int uw_version = (int) version_arg;
1098 _Unwind_Action uw_phases = (_Unwind_Action) phases_arg;
1099 region_descriptor region;
1100 action_descriptor action;
1102 /* Check that we're called from the ABI context we expect, with a major
1103 possible variation on VMS for IA64. */
1104 if (uw_version != 1)
1106 #if defined (VMS) && defined (__IA64)
1108 /* Assume we're called with sigargs/mechargs arguments if really
1109 unexpected bits are set in our first two formals. Redirect to the
1110 GNAT condition handling code in this case. */
1112 extern long __gnat_handle_vms_condition (void *, void *);
1114 unsigned int version_unexpected_bits_mask = 0xffffff00U;
1115 unsigned int phases_unexpected_bits_mask = 0xffffff00U;
1117 if ((unsigned int)uw_version & version_unexpected_bits_mask
1118 && (unsigned int)uw_phases & phases_unexpected_bits_mask)
1119 return __gnat_handle_vms_condition (version_arg, phases_arg);
1122 return _URC_FATAL_PHASE1_ERROR;
1125 db_indent (DB_INDENT_RESET);
1126 db_phases (uw_phases);
1127 db_indent (DB_INDENT_INCREASE);
1129 /* Get the region description for the context we were provided with. This
1130 will tell us if there is some lsda, call_site, action and/or ttype data
1131 for the associated ip. */
1132 get_region_description_for (uw_context, ®ion);
1133 db_region_for (®ion, uw_context);
1135 /* No LSDA => no handlers or cleanups => we shall unwind further up. */
1137 return _URC_CONTINUE_UNWIND;
1139 /* Search the call-site and action-record tables for the action associated
1141 get_action_description_for (uw_context, uw_exception, ®ion, &action);
1142 db_action_for (&action, uw_context);
1144 /* Whatever the phase, if there is nothing relevant in this frame,
1145 unwinding should just go on. */
1146 if (action.kind == nothing)
1147 return _URC_CONTINUE_UNWIND;
1149 /* If we found something in search phase, we should return a code indicating
1150 what to do next depending on what we found. If we only have cleanups
1151 around, we shall try to unwind further up to find a handler, otherwise,
1152 tell we have a handler, which will trigger the second phase. */
1153 if (uw_phases & _UA_SEARCH_PHASE)
1155 if (action.kind == cleanup)
1157 return _URC_CONTINUE_UNWIND;
1161 /* Trigger the appropriate notification routines before the second
1162 phase starts, which ensures the stack is still intact. */
1163 __gnat_notify_handled_exception ();
1165 return _URC_HANDLER_FOUND;
1169 /* We found something in cleanup/handler phase, which might be the handler
1170 or a cleanup for a handled occurrence, or a cleanup for an unhandled
1171 occurrence (we are in a FORCED_UNWIND phase in this case). Install the
1172 context to get there. */
1175 (uw_context, uw_exception, action.landing_pad, action.ttype_filter);
1177 /* Write current exception, so that it can be retrieved from Ada. */
1178 __gnat_setup_current_excep (uw_exception);
1180 return _URC_INSTALL_CONTEXT;
1183 /* Define the consistently named wrappers imported by Propagate_Exception. */
1185 #ifdef __USING_SJLJ_EXCEPTIONS__
1187 #undef _Unwind_RaiseException
1190 __gnat_Unwind_RaiseException (_Unwind_Exception *e)
1192 return _Unwind_SjLj_RaiseException (e);
1196 #undef _Unwind_ForcedUnwind
1199 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *e,
1203 return _Unwind_SjLj_ForcedUnwind (e, handler, argument);
1207 #else /* __USING_SJLJ_EXCEPTIONS__ */
1210 __gnat_Unwind_RaiseException (_Unwind_Exception *e)
1212 return _Unwind_RaiseException (e);
1216 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *e,
1220 return _Unwind_ForcedUnwind (e, handler, argument);
1223 #endif /* __USING_SJLJ_EXCEPTIONS__ */
1228 /* Define the corresponding stubs for the compiler. */
1230 /* We don't want fancy_abort here. */
1234 __gnat_Unwind_RaiseException (_Unwind_Exception *e ATTRIBUTE_UNUSED)
1241 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *e ATTRIBUTE_UNUSED,
1242 void * handler ATTRIBUTE_UNUSED,
1243 void * argument ATTRIBUTE_UNUSED)