1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 1992-2013, 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
36 #error "RTS unit only"
50 /* On MacOS X, versions older than 10.5 don't export _Unwind_GetIPInfo. */
52 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
53 #define HAVE_GETIPINFO 1
57 #if defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
58 /* HP-UX B.11.31 ia64 libunwind doesn't have _Unwind_GetIPInfo. */
60 #define _UA_END_OF_STACK 0
63 /* The names of a couple of "standard" routines for unwinding/propagation
64 actually vary depending on the underlying GCC scheme for exception handling
65 (SJLJ or DWARF). We need a consistently named interface to import from
66 a-except, so wrappers are defined here. */
70 typedef struct _Unwind_Context _Unwind_Context
;
71 typedef struct _Unwind_Exception _Unwind_Exception
;
74 __gnat_Unwind_RaiseException (_Unwind_Exception
*);
77 __gnat_Unwind_ForcedUnwind (_Unwind_Exception
*, void *, void *);
79 extern struct Exception_Occurrence
*__gnat_setup_current_excep
80 (_Unwind_Exception
*);
81 extern void __gnat_unhandled_except_handler (_Unwind_Exception
*);
83 #include "unwind-pe.h"
85 /* The known and handled exception classes. */
87 #define CXX_EXCEPTION_CLASS 0x474e5543432b2b00ULL
88 #define GNAT_EXCEPTION_CLASS 0x474e552d41646100ULL
90 /* --------------------------------------------------------------
91 -- The DB stuff below is there for debugging purposes only. --
92 -------------------------------------------------------------- */
98 #define DB_ACTIONS 0x4
99 #define DB_REGIONS 0x8
101 #define DB_ERR 0x1000
103 /* The "action" stuff below is also there for debugging purposes only. */
107 _Unwind_Action phase
;
108 const char * description
;
111 static const phase_descriptor phase_descriptors
[]
112 = {{ _UA_SEARCH_PHASE
, "SEARCH_PHASE" },
113 { _UA_CLEANUP_PHASE
, "CLEANUP_PHASE" },
114 { _UA_HANDLER_FRAME
, "HANDLER_FRAME" },
115 { _UA_FORCE_UNWIND
, "FORCE_UNWIND" },
119 db_accepted_codes (void)
121 static int accepted_codes
= -1;
123 if (accepted_codes
== -1)
125 char * db_env
= (char *) getenv ("EH_DEBUG");
127 accepted_codes
= db_env
? (atoi (db_env
) | DB_ERR
) : 0;
128 /* Arranged for ERR stuff to always be visible when the variable
129 is defined. One may just set the variable to 0 to see the ERR
133 return accepted_codes
;
136 #define DB_INDENT_INCREASE 0x01
137 #define DB_INDENT_DECREASE 0x02
138 #define DB_INDENT_OUTPUT 0x04
139 #define DB_INDENT_NEWLINE 0x08
140 #define DB_INDENT_RESET 0x10
142 #define DB_INDENT_UNIT 8
145 db_indent (int requests
)
147 static int current_indentation_level
= 0;
149 if (requests
& DB_INDENT_RESET
)
150 current_indentation_level
= 0;
152 if (requests
& DB_INDENT_INCREASE
)
153 current_indentation_level
++;
155 if (requests
& DB_INDENT_DECREASE
)
156 current_indentation_level
--;
158 if (requests
& DB_INDENT_NEWLINE
)
159 fprintf (stderr
, "\n");
161 if (requests
& DB_INDENT_OUTPUT
)
162 fprintf (stderr
, "%*s", current_indentation_level
* DB_INDENT_UNIT
, " ");
165 static void ATTRIBUTE_PRINTF_2
166 db (int db_code
, char * msg_format
, ...)
168 if (db_accepted_codes () & db_code
)
172 db_indent (DB_INDENT_OUTPUT
);
174 va_start (msg_args
, msg_format
);
175 vfprintf (stderr
, msg_format
, msg_args
);
181 db_phases (int phases
)
183 const phase_descriptor
*a
= phase_descriptors
;
185 if (! (db_accepted_codes() & DB_PHASES
))
188 db (DB_PHASES
, "\n");
190 for (; a
->description
!= 0; a
++)
191 if (phases
& a
->phase
)
192 db (DB_PHASES
, "%s ", a
->description
);
194 db (DB_PHASES
, " :\n");
196 #else /* !inhibit_libc */
200 #endif /* !inhibit_libc */
202 /* ---------------------------------------------------------------
203 -- Now come a set of useful structures and helper routines. --
204 --------------------------------------------------------------- */
206 /* There are three major runtime tables involved, generated by the
207 GCC back-end. Contents slightly vary depending on the underlying
208 implementation scheme (dwarf zero cost / sjlj).
210 =======================================
211 * Tables for the dwarf zero cost case *
212 =======================================
214 They are fully documented in:
215 http://sourcery.mentor.com/public/cxx-abi/exceptions.pdf
216 Here is a shorter presentation, with some specific comments for Ada.
219 -------------------------------------------------------------------
220 * region-start | region-length | landing-pad | first-action-index *
221 -------------------------------------------------------------------
223 Identify possible actions to be taken and where to resume control
224 for that when an exception propagates through a pc inside the region
225 delimited by start and length.
227 A null landing-pad indicates that nothing is to be done.
229 Otherwise, first-action-index provides an entry into the action[]
230 table which heads a list of possible actions to be taken (see below).
232 If it is determined that indeed an action should be taken, that
233 is, if one action filter matches the exception being propagated,
234 then control should be transferred to landing-pad.
236 A null first-action-index indicates that there are only cleanups
240 -------------------------------
241 * action-filter | next-action *
242 -------------------------------
244 This table contains lists (called action chains) of possible actions
245 associated with call-site entries described in the call-site [] table.
246 There is at most one action list per call-site entry. It is SLEB128
249 A null action-filter indicates a cleanup.
251 Non null action-filters provide an index into the ttypes [] table
252 (see below), from which information may be retrieved to check if it
253 matches the exception being propagated.
256 means there is a regular handler to be run The value is also passed
257 to the landing pad to dispatch the exception.
260 means there is a some "exception_specification" data to retrieve,
261 which is only relevant for C++ and should never show up for Ada.
262 (Exception specification specifies which exceptions can be thrown
263 by a function. Such filter is emitted around the body of C++
264 functions defined like:
265 void foo ([...]) throw (A, B) { [...] }
266 These can be viewed as negativ filter: the landing pad is branched
267 to for exceptions that doesn't match the filter and usually aborts
271 points to the next entry in the list using a relative byte offset. 0
272 indicates there is no other entry.
279 This table is an array of addresses.
281 A null value indicates a catch-all handler. (Not used by Ada)
283 Non null values are used to match the exception being propagated:
284 In C++ this is a pointer to some rtti data, while in Ada this is an
285 exception id (with a fake id for others).
287 For C++, this table is actually also used to store "exception
288 specification" data. The differentiation between the two kinds
289 of entries is made by the sign of the associated action filter,
290 which translates into positive or negative offsets from the
291 so called base of the table:
293 Exception Specification data is stored at positive offsets from
294 the ttypes table base, which Exception Type data is stored at
297 ---------------------------------------------------------------------------
299 Here is a quick summary of the tables organization:
301 +-- Unwind_Context (pc, ...)
307 | +=============================================================+
308 | | region-start + length | landing-pad | first-action-index |
309 | +=============================================================+
310 +-> | pc range 0 => no-action 0 => cleanups only |
311 | !0 => jump @ N --+ |
312 +====================================================== | ====+
317 +==========================================================+ |
318 | action-filter | next-action | |
319 +==========================================================+ |
321 | >0 => ttype index for handler ------+ 0 => end of chain | <-+
322 | <0 => ttype index for spec data | |
323 +==================================== | ===================+
327 | Offset negated from
328 +=====================+ | the actual base.
330 +============+=====================+ |
332 | ... | exception id | <---+
334 | handlers +---------------------+
338 +============+=====================+ <<------ Table base
340 | specs | ... | (should not see negative filter
341 | ... | ... | values for Ada).
342 +============+=====================+
345 ============================
346 * Tables for the sjlj case *
347 ============================
349 So called "function contexts" are pushed on a context stack by calls to
350 _Unwind_SjLj_Register on function entry, and popped off at exit points by
351 calls to _Unwind_SjLj_Unregister. The current call_site for a function is
352 updated in the function context as the function's code runs along.
354 The generic unwinding engine in _Unwind_RaiseException walks the function
355 context stack and not the actual call chain.
357 The ACTION and TTYPES tables remain unchanged, which allows to search them
358 during the propagation phase to determine whether or not the propagated
359 exception is handled somewhere. When it is, we only "jump" up once directly
360 to the context where the handler will be found. Besides, this allows "break
361 exception unhandled" to work also
363 The CALL-SITE table is setup differently, though: the pc attached to the
364 unwind context is a direct index into the table, so the entries in this
365 table do not hold region bounds any more.
367 A special index (-1) is used to indicate that no action is possibly
368 connected with the context at hand, so null landing pads cannot appear
371 Additionally, landing pad values in the table do not represent code address
372 to jump at, but so called "dispatch" indices used by a common landing pad
373 for the function to switch to the appropriate post-landing-pad.
375 +-- Unwind_Context (pc, ...)
377 | pc = call-site index
378 | 0 => terminate (should not see this for Ada)
383 | +=====================================+
384 | | landing-pad | first-action-index |
385 | +=====================================+
386 +-> | 0 => cleanups only |
388 +=====================================+
391 ===================================
392 * Basic organization of this unit *
393 ===================================
395 The major point of this unit is to provide an exception propagation
396 personality routine for Ada. This is __gnat_personality_v0.
398 It is provided with a pointer to the propagated exception, an unwind
399 context describing a location the propagation is going through, and a
400 couple of other arguments including a description of the current
403 It shall return to the generic propagation engine what is to be performed
404 next, after possible context adjustments, depending on what it finds in the
405 traversed context (a handler for the exception, a cleanup, nothing, ...),
406 and on the propagation phase.
408 A number of structures and subroutines are used for this purpose, as
411 o region_descriptor: General data associated with the context (base pc,
412 call-site table, action table, ttypes table, ...)
414 o action_descriptor: Data describing the action to be taken for the
415 propagated exception in the provided context (kind of action: nothing,
416 handler, cleanup; pointer to the action table entry, ...).
422 Propagate_Exception (a-exexpr.adb)
425 _Unwind_RaiseException (libgcc)
429 +--> __gnat_personality_v0 (context, exception)
431 +--> get_region_description_for (context)
433 +--> get_action_description_for (ip, exception, region)
435 | +--> get_call_site_action_for (context, region)
436 | (one version for each underlying scheme)
438 +--> setup_to_install (context)
440 This unit is inspired from the C++ version found in eh_personality.cc,
441 part of libstdc++-v3.
446 /* This is an incomplete "proxy" of the structure of exception objects as
447 built by the GNAT runtime library. Accesses to other fields than the common
448 header are performed through subprogram calls to alleviate the need of an
449 exact counterpart here and potential alignment/size issues for the common
450 header. See a-exexpr.adb. */
454 _Unwind_Exception common
;
455 /* ABI header, maximally aligned. */
458 /* The two constants below are specific ttype identifiers for special
459 exception ids. Their type should match what a-exexpr exports. */
461 extern const int __gnat_others_value
;
462 #define GNAT_OTHERS ((_Unwind_Ptr) &__gnat_others_value)
464 extern const int __gnat_all_others_value
;
465 #define GNAT_ALL_OTHERS ((_Unwind_Ptr) &__gnat_all_others_value)
467 extern const int __gnat_unhandled_others_value
;
468 #define GNAT_UNHANDLED_OTHERS ((_Unwind_Ptr) &__gnat_unhandled_others_value)
470 /* Describe the useful region data associated with an unwind context. */
474 /* The base pc of the region. */
477 /* Pointer to the Language Specific Data for the region. */
480 /* Call-Site data associated with this region. */
481 unsigned char call_site_encoding
;
482 const unsigned char *call_site_table
;
484 /* The base to which are relative landing pad offsets inside the call-site
488 /* Action-Table associated with this region. */
489 const unsigned char *action_table
;
491 /* Ttype data associated with this region. */
492 unsigned char ttype_encoding
;
493 const unsigned char *ttype_table
;
494 _Unwind_Ptr ttype_base
;
498 /* Extract and adjust the IP (instruction pointer) from an exception
502 get_ip_from_context (_Unwind_Context
*uw_context
)
504 int ip_before_insn
= 0;
505 #ifdef HAVE_GETIPINFO
506 _Unwind_Ptr ip
= _Unwind_GetIPInfo (uw_context
, &ip_before_insn
);
508 _Unwind_Ptr ip
= _Unwind_GetIP (uw_context
);
510 /* Subtract 1 if necessary because GetIPInfo yields a call return address
511 in this case, while we are interested in information for the call point.
512 This does not always yield the exact call instruction address but always
513 brings the IP back within the corresponding region. */
521 db_region_for (region_descriptor
*region
, _Unwind_Ptr ip
)
524 if (! (db_accepted_codes () & DB_REGIONS
))
527 db (DB_REGIONS
, "For ip @ %p => ", (void *)ip
);
530 db (DB_REGIONS
, "lsda @ %p", (void *)region
->lsda
);
532 db (DB_REGIONS
, "no lsda");
534 db (DB_REGIONS
, "\n");
538 /* Retrieve the ttype entry associated with FILTER in the REGION's
542 get_ttype_entry_for (region_descriptor
*region
, long filter
)
544 _Unwind_Ptr ttype_entry
;
546 filter
*= size_of_encoded_value (region
->ttype_encoding
);
547 read_encoded_value_with_base
548 (region
->ttype_encoding
, region
->ttype_base
,
549 region
->ttype_table
- filter
, &ttype_entry
);
554 /* Fill out the REGION descriptor for the provided UW_CONTEXT. */
557 get_region_description_for (_Unwind_Context
*uw_context
,
558 region_descriptor
*region
)
560 const unsigned char * p
;
562 unsigned char lpbase_encoding
;
564 /* Get the base address of the lsda information. If the provided context
565 is null or if there is no associated language specific data, there's
566 nothing we can/should do. */
568 = (_Unwind_Ptr
) (uw_context
569 ? _Unwind_GetLanguageSpecificData (uw_context
) : 0);
574 /* Parse the lsda and fill the region descriptor. */
575 p
= (const unsigned char *)region
->lsda
;
577 region
->base
= _Unwind_GetRegionStart (uw_context
);
579 /* Find @LPStart, the base to which landing pad offsets are relative. */
580 lpbase_encoding
= *p
++;
581 if (lpbase_encoding
!= DW_EH_PE_omit
)
582 p
= read_encoded_value
583 (uw_context
, lpbase_encoding
, p
, ®ion
->lp_base
);
585 region
->lp_base
= region
->base
;
587 /* Find @TType, the base of the handler and exception spec type data. */
588 region
->ttype_encoding
= *p
++;
589 if (region
->ttype_encoding
!= DW_EH_PE_omit
)
591 p
= read_uleb128 (p
, &tmp
);
592 region
->ttype_table
= p
+ tmp
;
595 region
->ttype_table
= 0;
598 = base_of_encoded_value (region
->ttype_encoding
, uw_context
);
600 /* Get the encoding and length of the call-site table; the action table
601 immediately follows. */
602 region
->call_site_encoding
= *p
++;
603 region
->call_site_table
= read_uleb128 (p
, &tmp
);
605 region
->action_table
= region
->call_site_table
+ tmp
;
609 /* Describe an action to be taken when propagating an exception up to
614 /* Found some call site base data, but need to analyze further
615 before being able to decide. */
618 /* There is nothing relevant in the context at hand. */
621 /* There are only cleanups to run in this context. */
624 /* There is a handler for the exception in this context. */
627 /* There is a handler for the exception, but it is only for catching
628 unhandled exceptions. */
632 /* filter value for cleanup actions. */
633 static const int cleanup_filter
= 0;
637 /* The kind of action to be taken. */
638 enum action_kind kind
;
640 /* A pointer to the action record entry. */
641 const unsigned char *table_entry
;
643 /* Where we should jump to actually take an action (trigger a cleanup or an
644 exception handler). */
645 _Unwind_Ptr landing_pad
;
647 /* If we have a handler matching our exception, these are the filter to
648 trigger it and the corresponding id. */
649 _Unwind_Sword ttype_filter
;
654 db_action_for (action_descriptor
*action
, _Unwind_Ptr ip
)
657 db (DB_ACTIONS
, "For ip @ %p => ", (void *)ip
);
659 switch (action
->kind
)
662 db (DB_ACTIONS
, "lpad @ %p, record @ %p\n",
663 (void *) action
->landing_pad
, action
->table_entry
);
667 db (DB_ACTIONS
, "Nothing\n");
671 db (DB_ACTIONS
, "Cleanup\n");
675 db (DB_ACTIONS
, "Handler, filter = %d\n", (int) action
->ttype_filter
);
679 db (DB_ACTIONS
, "Err? Unexpected action kind !\n");
685 /* Search the call_site_table of REGION for an entry appropriate for the
686 UW_CONTEXT's IP. If one is found, store the associated landing_pad
687 and action_table entry, and set the ACTION kind to unknown for further
688 analysis. Otherwise, set the ACTION kind to nothing.
690 There are two variants of this routine, depending on the underlying
691 mechanism (DWARF/SJLJ), which account for differences in the tables. */
693 #ifdef __USING_SJLJ_EXCEPTIONS__
695 #define __builtin_eh_return_data_regno(x) x
698 get_call_site_action_for (_Unwind_Ptr call_site
,
699 region_descriptor
*region
,
700 action_descriptor
*action
)
702 /* call_site is a direct index into the call-site table, with two special
703 values : -1 for no-action and 0 for "terminate". The latter should never
704 show up for Ada. To test for the former, beware that _Unwind_Ptr might
707 if ((int)call_site
< 0)
709 action
->kind
= nothing
;
711 else if (call_site
== 0)
713 db (DB_ERR
, "========> Err, null call_site for Ada/sjlj\n");
714 action
->kind
= nothing
;
718 _uleb128_t cs_lp
, cs_action
;
719 const unsigned char *p
;
721 /* Let the caller know there may be an action to take, but let it
722 determine the kind. */
723 action
->kind
= unknown
;
725 /* We have a direct index into the call-site table, but this table is
726 made of leb128 values, the encoding length of which is variable. We
727 can't merely compute an offset from the index, then, but have to read
728 all the entries before the one of interest. */
729 p
= region
->call_site_table
;
732 p
= read_uleb128 (p
, &cs_lp
);
733 p
= read_uleb128 (p
, &cs_action
);
737 action
->landing_pad
= cs_lp
+ 1;
740 action
->table_entry
= region
->action_table
+ cs_action
- 1;
742 action
->table_entry
= 0;
746 #else /* !__USING_SJLJ_EXCEPTIONS__ */
749 get_call_site_action_for (_Unwind_Ptr ip
,
750 region_descriptor
*region
,
751 action_descriptor
*action
)
753 const unsigned char *p
= region
->call_site_table
;
755 /* Unless we are able to determine otherwise... */
756 action
->kind
= nothing
;
760 while (p
< region
->action_table
)
762 _Unwind_Ptr cs_start
, cs_len
, cs_lp
;
763 _uleb128_t cs_action
;
765 /* Note that all call-site encodings are "absolute" displacements. */
766 p
= read_encoded_value (0, region
->call_site_encoding
, p
, &cs_start
);
767 p
= read_encoded_value (0, region
->call_site_encoding
, p
, &cs_len
);
768 p
= read_encoded_value (0, region
->call_site_encoding
, p
, &cs_lp
);
769 p
= read_uleb128 (p
, &cs_action
);
772 "c_site @ %p (+%p), len = %p, lpad @ %p (+%p)\n",
773 (void *)region
->base
+ cs_start
, (void *)cs_start
, (void *)cs_len
,
774 (void *)region
->lp_base
+ cs_lp
, (void *)cs_lp
);
776 /* The table is sorted, so if we've passed the IP, stop. */
777 if (ip
< region
->base
+ cs_start
)
780 /* If we have a match, fill the ACTION fields accordingly. */
781 else if (ip
< region
->base
+ cs_start
+ cs_len
)
783 /* Let the caller know there may be an action to take, but let it
784 determine the kind. */
785 action
->kind
= unknown
;
788 action
->landing_pad
= region
->lp_base
+ cs_lp
;
790 action
->landing_pad
= 0;
793 action
->table_entry
= region
->action_table
+ cs_action
- 1;
795 action
->table_entry
= 0;
797 db (DB_CSITE
, "+++\n");
802 db (DB_CSITE
, "---\n");
805 #endif /* __USING_SJLJ_EXCEPTIONS__ */
807 /* With CHOICE an exception choice representing an "exception - when"
808 argument, and PROPAGATED_EXCEPTION a pointer to the currently propagated
809 occurrence, return true if the latter matches the former, that is, if
810 PROPAGATED_EXCEPTION is caught by the handling code controlled by CHOICE.
811 This takes care of the special Non_Ada_Error case on VMS. */
813 #define Is_Handled_By_Others __gnat_is_handled_by_others
814 #define Language_For __gnat_language_for
815 #define Import_Code_For __gnat_import_code_for
816 #define EID_For __gnat_eid_for
818 extern bool Is_Handled_By_Others (_Unwind_Ptr eid
);
819 extern char Language_For (_Unwind_Ptr eid
);
821 extern Exception_Code
Import_Code_For (_Unwind_Ptr eid
);
823 extern Exception_Id
EID_For (_GNAT_Exception
* e
);
825 static enum action_kind
826 is_handled_by (_Unwind_Ptr choice
, _GNAT_Exception
* propagated_exception
)
828 if (choice
== GNAT_ALL_OTHERS
)
831 if (propagated_exception
->common
.exception_class
== GNAT_EXCEPTION_CLASS
)
833 /* Pointer to the GNAT exception data corresponding to the propagated
835 _Unwind_Ptr E
= (_Unwind_Ptr
) EID_For (propagated_exception
);
837 if (choice
== GNAT_UNHANDLED_OTHERS
)
840 E
= (_Unwind_Ptr
) EID_For (propagated_exception
);
842 /* Base matching rules: An exception data (id) matches itself, "when
843 all_others" matches anything and "when others" matches anything
844 unless explicitly stated otherwise in the propagated occurrence. */
845 if (choice
== E
|| (choice
== GNAT_OTHERS
&& Is_Handled_By_Others (E
)))
848 /* In addition, on OpenVMS, Non_Ada_Error matches VMS exceptions, and we
849 may have different exception data pointers that should match for the
850 same condition code, if both an export and an import have been
851 registered. The import code for both the choice and the propagated
852 occurrence are expected to have been masked off regarding severity
853 bits already (at registration time for the former and from within the
854 low level exception vector for the latter). */
856 # define Non_Ada_Error system__aux_dec__non_ada_error
857 extern struct Exception_Data Non_Ada_Error
;
859 if ((Language_For (E
) == 'V'
860 && choice
!= GNAT_OTHERS
861 && ((Language_For (choice
) == 'V'
862 && Import_Code_For (choice
) != 0
863 && Import_Code_For (choice
) == Import_Code_For (E
))
864 || choice
== (_Unwind_Ptr
)&Non_Ada_Error
)))
870 # define Foreign_Exception system__exceptions__foreign_exception
871 extern struct Exception_Data Foreign_Exception
;
873 if (choice
== GNAT_ALL_OTHERS
874 || choice
== GNAT_OTHERS
875 || choice
== (_Unwind_Ptr
) &Foreign_Exception
)
881 /* Fill out the ACTION to be taken from propagating UW_EXCEPTION up to
882 UW_CONTEXT in REGION. */
885 get_action_description_for (_Unwind_Ptr ip
,
886 _Unwind_Exception
*uw_exception
,
887 _Unwind_Action uw_phase
,
888 region_descriptor
*region
,
889 action_descriptor
*action
)
891 _GNAT_Exception
*gnat_exception
= (_GNAT_Exception
*) uw_exception
;
893 /* Search the call site table first, which may get us a landing pad as well
894 as the head of an action record list. */
895 get_call_site_action_for (ip
, region
, action
);
896 db_action_for (action
, ip
);
898 /* If there is not even a call_site entry, we are done. */
899 if (action
->kind
== nothing
)
902 /* Otherwise, check what we have at the place of the call site. */
904 /* No landing pad => no cleanups or handlers. */
905 if (action
->landing_pad
== 0)
907 action
->kind
= nothing
;
911 /* Landing pad + null table entry => only cleanups. */
912 else if (action
->table_entry
== 0)
914 action
->kind
= cleanup
;
915 action
->ttype_filter
= cleanup_filter
;
916 /* The filter initialization is not strictly necessary, as cleanup-only
917 landing pads don't look at the filter value. It is there to ensure
918 we don't pass random values and so trigger potential confusion when
919 installing the context later on. */
923 /* Landing pad + Table entry => handlers + possible cleanups. */
926 const unsigned char * p
= action
->table_entry
;
928 _sleb128_t ar_filter
, ar_disp
;
930 action
->kind
= nothing
;
934 p
= read_sleb128 (p
, &ar_filter
);
935 read_sleb128 (p
, &ar_disp
);
936 /* Don't assign p here, as it will be incremented by ar_disp
939 /* Null filters are for cleanups. */
940 if (ar_filter
== cleanup_filter
)
942 action
->kind
= cleanup
;
943 action
->ttype_filter
= cleanup_filter
;
944 /* The filter initialization is required here, to ensure
945 the target landing pad branches to the cleanup code if
946 we happen not to find a matching handler. */
949 /* Positive filters are for regular handlers. */
950 else if (ar_filter
> 0)
952 /* Do not catch an exception if the _UA_FORCE_UNWIND flag is
953 passed (to follow the ABI). */
954 if (!(uw_phase
& _UA_FORCE_UNWIND
))
956 enum action_kind act
;
958 /* See if the filter we have is for an exception which
959 matches the one we are propagating. */
960 _Unwind_Ptr choice
= get_ttype_entry_for (region
, ar_filter
);
962 act
= is_handled_by (choice
, gnat_exception
);
966 action
->ttype_filter
= ar_filter
;
972 /* Negative filter values are for C++ exception specifications.
973 Should not be there for Ada :/ */
975 db (DB_ERR
, "========> Err, filter < 0 for Ada/dwarf\n");
985 /* Setup in UW_CONTEXT the eh return target IP and data registers, which will
986 be restored with the others and retrieved by the landing pad once the jump
990 setup_to_install (_Unwind_Context
*uw_context
,
991 _Unwind_Exception
*uw_exception
,
992 _Unwind_Ptr uw_landing_pad
,
995 /* 1/ exception object pointer, which might be provided back to
996 _Unwind_Resume (and thus to this personality routine) if we are jumping
998 _Unwind_SetGR (uw_context
, __builtin_eh_return_data_regno (0),
999 (_Unwind_Word
)uw_exception
);
1001 /* 2/ handler switch value register, which will also be used by the target
1002 landing pad to decide what action it shall take. */
1003 _Unwind_SetGR (uw_context
, __builtin_eh_return_data_regno (1),
1004 (_Unwind_Word
)uw_filter
);
1006 /* Setup the address we should jump at to reach the code where there is the
1007 "something" we found. */
1008 _Unwind_SetIP (uw_context
, uw_landing_pad
);
1011 /* The following is defined from a-except.adb. Its purpose is to enable
1012 automatic backtraces upon exception raise, as provided through the
1013 GNAT.Traceback facilities. */
1014 extern void __gnat_notify_handled_exception (struct Exception_Occurrence
*);
1015 extern void __gnat_notify_unhandled_exception (struct Exception_Occurrence
*);
1017 /* Below is the eh personality routine per se. We currently assume that only
1018 GNU-Ada exceptions are met. */
1020 #ifdef __USING_SJLJ_EXCEPTIONS__
1021 #define PERSONALITY_FUNCTION __gnat_personality_sj0
1022 #elif defined (__SEH__)
1023 #define PERSONALITY_FUNCTION __gnat_personality_imp
1025 #define PERSONALITY_FUNCTION __gnat_personality_v0
1028 /* Major tweak for ia64-vms : the CHF propagation phase calls this personality
1029 routine with sigargs/mechargs arguments and has very specific expectations
1030 on possible return values.
1032 We handle this with a number of specific tricks:
1034 1. We tweak the personality routine prototype to have the "version" and
1035 "phases" two first arguments be void * instead of int and _Unwind_Action
1036 as nominally expected in the GCC context.
1038 This allows us to access the full range of bits passed in every case and
1039 has no impact on the callers side since each argument remains assigned
1040 the same single 64bit slot.
1042 2. We retrieve the corresponding int and _Unwind_Action values within the
1043 routine for regular use with truncating conversions. This is a noop when
1044 called from the libgcc unwinder.
1046 3. We assume we're called by the VMS CHF when unexpected bits are set in
1047 both those values. The incoming arguments are then real sigargs and
1048 mechargs pointers, which we then redirect to __gnat_handle_vms_condition
1049 for proper processing.
1051 #if defined (VMS) && defined (__IA64)
1052 typedef void * version_arg_t
;
1053 typedef void * phases_arg_t
;
1055 typedef int version_arg_t
;
1056 typedef _Unwind_Action phases_arg_t
;
1059 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
1063 PERSONALITY_FUNCTION (version_arg_t
, phases_arg_t
,
1064 _Unwind_Exception_Class
, _Unwind_Exception
*,
1068 PERSONALITY_FUNCTION (version_arg_t version_arg
,
1069 phases_arg_t phases_arg
,
1070 _Unwind_Exception_Class uw_exception_class
1072 _Unwind_Exception
*uw_exception
,
1073 _Unwind_Context
*uw_context
)
1075 /* Fetch the version and phases args with their nominal ABI types for later
1076 use. This is a noop everywhere except on ia64-vms when called from the
1077 Condition Handling Facility. */
1078 int uw_version
= (int) version_arg
;
1079 _Unwind_Action uw_phases
= (_Unwind_Action
) phases_arg
;
1080 region_descriptor region
;
1081 action_descriptor action
;
1084 /* Check that we're called from the ABI context we expect, with a major
1085 possible variation on VMS for IA64. */
1086 if (uw_version
!= 1)
1088 #if defined (VMS) && defined (__IA64)
1090 /* Assume we're called with sigargs/mechargs arguments if really
1091 unexpected bits are set in our first two formals. Redirect to the
1092 GNAT condition handling code in this case. */
1094 extern long __gnat_handle_vms_condition (void *, void *);
1096 unsigned int version_unexpected_bits_mask
= 0xffffff00U
;
1097 unsigned int phases_unexpected_bits_mask
= 0xffffff00U
;
1099 if ((unsigned int)uw_version
& version_unexpected_bits_mask
1100 && (unsigned int)uw_phases
& phases_unexpected_bits_mask
)
1101 return __gnat_handle_vms_condition (version_arg
, phases_arg
);
1104 return _URC_FATAL_PHASE1_ERROR
;
1107 db_indent (DB_INDENT_RESET
);
1108 db_phases (uw_phases
);
1109 db_indent (DB_INDENT_INCREASE
);
1111 /* Get the region description for the context we were provided with. This
1112 will tell us if there is some lsda, call_site, action and/or ttype data
1113 for the associated ip. */
1114 get_region_description_for (uw_context
, ®ion
);
1115 ip
= get_ip_from_context (uw_context
);
1116 db_region_for (®ion
, ip
);
1118 /* No LSDA => no handlers or cleanups => we shall unwind further up. */
1120 return _URC_CONTINUE_UNWIND
;
1122 /* Search the call-site and action-record tables for the action associated
1124 get_action_description_for (ip
, uw_exception
, uw_phases
, ®ion
, &action
);
1125 db_action_for (&action
, ip
);
1127 /* Whatever the phase, if there is nothing relevant in this frame,
1128 unwinding should just go on. */
1129 if (action
.kind
== nothing
)
1130 return _URC_CONTINUE_UNWIND
;
1132 /* If we found something in search phase, we should return a code indicating
1133 what to do next depending on what we found. If we only have cleanups
1134 around, we shall try to unwind further up to find a handler, otherwise,
1135 tell we have a handler, which will trigger the second phase. */
1136 if (uw_phases
& _UA_SEARCH_PHASE
)
1138 if (action
.kind
== cleanup
)
1140 return _URC_CONTINUE_UNWIND
;
1144 struct Exception_Occurrence
*excep
;
1146 /* Trigger the appropriate notification routines before the second
1147 phase starts, which ensures the stack is still intact.
1148 First, setup the Ada occurrence. */
1149 excep
= __gnat_setup_current_excep (uw_exception
);
1150 if (action
.kind
== unhandler
)
1151 __gnat_notify_unhandled_exception (excep
);
1153 __gnat_notify_handled_exception (excep
);
1155 return _URC_HANDLER_FOUND
;
1159 /* We found something in cleanup/handler phase, which might be the handler
1160 or a cleanup for a handled occurrence, or a cleanup for an unhandled
1161 occurrence (we are in a FORCED_UNWIND phase in this case). Install the
1162 context to get there. */
1165 (uw_context
, uw_exception
, action
.landing_pad
, action
.ttype_filter
);
1167 /* Write current exception, so that it can be retrieved from Ada. */
1168 __gnat_setup_current_excep (uw_exception
);
1170 return _URC_INSTALL_CONTEXT
;
1173 /* Callback routine called by Unwind_ForcedUnwind to execute all the cleanup
1174 before exiting the task. */
1177 __gnat_cleanupunwind_handler (int version ATTRIBUTE_UNUSED
,
1178 _Unwind_Action phases
,
1179 _Unwind_Exception_Class eclass ATTRIBUTE_UNUSED
,
1180 struct _Unwind_Exception
*exception
,
1181 struct _Unwind_Context
*context ATTRIBUTE_UNUSED
,
1182 void *arg ATTRIBUTE_UNUSED
)
1184 /* Terminate when the end of the stack is reached. */
1185 if ((phases
& _UA_END_OF_STACK
) != 0
1186 #if defined (__ia64__) && defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
1187 /* Strictely follow the ia64 ABI: when end of stack is reached,
1188 the callback will be called with a NULL stack pointer.
1189 No need for that when using libgcc unwinder. */
1190 || _Unwind_GetGR (context
, 12) == 0
1193 __gnat_unhandled_except_handler (exception
);
1195 /* We know there is at least one cleanup further up. Return so that it
1196 is searched and entered, after which Unwind_Resume will be called
1197 and this hook will gain control again. */
1198 return _URC_NO_REASON
;
1201 /* Define the consistently named wrappers imported by Propagate_Exception. */
1204 __gnat_Unwind_RaiseException (_Unwind_Exception
*e
)
1206 #ifdef __USING_SJLJ_EXCEPTIONS__
1207 return _Unwind_SjLj_RaiseException (e
);
1209 return _Unwind_RaiseException (e
);
1214 __gnat_Unwind_ForcedUnwind (_Unwind_Exception
*e
,
1218 #ifdef __USING_SJLJ_EXCEPTIONS__
1219 return _Unwind_SjLj_ForcedUnwind (e
, handler
, argument
);
1221 return _Unwind_ForcedUnwind (e
, handler
, argument
);
1225 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
1227 #define STATUS_USER_DEFINED (1U << 29)
1229 /* From unwind-seh.c. */
1230 #define GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C')
1231 #define GCC_EXCEPTION(TYPE) \
1232 (STATUS_USER_DEFINED | ((TYPE) << 24) | GCC_MAGIC)
1233 #define STATUS_GCC_THROW GCC_EXCEPTION (0)
1235 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
1236 (struct _EXCEPTION_RECORD
*, void*, struct _CONTEXT
*, void*);
1238 struct Exception_Data
*
1239 __gnat_map_SEH (EXCEPTION_RECORD
* ExceptionRecord
, const char **msg
);
1241 struct _Unwind_Exception
*
1242 __gnat_create_machine_occurrence_from_signal_handler (Exception_Id
,
1245 /* Unwind opcodes. */
1246 #define UWOP_PUSH_NONVOL 0
1247 #define UWOP_ALLOC_LARGE 1
1248 #define UWOP_ALLOC_SMALL 2
1249 #define UWOP_SET_FPREG 3
1250 #define UWOP_SAVE_NONVOL 4
1251 #define UWOP_SAVE_NONVOL_FAR 5
1252 #define UWOP_SAVE_XMM128 8
1253 #define UWOP_SAVE_XMM128_FAR 9
1254 #define UWOP_PUSH_MACHFRAME 10
1256 /* Modify the IP value saved in the machine frame. This is really a kludge,
1257 that will be removed if we could propagate the Windows exception (and not
1259 What is very wrong is that the Windows unwinder will try to decode the
1260 instruction at IP, which isn't valid anymore after the adjust. */
1263 __gnat_adjust_context (unsigned char *unw
, ULONG64 rsp
)
1267 /* Version = 1, no flags, no prolog. */
1268 if (unw
[0] != 1 || unw
[1] != 0)
1271 /* No frame pointer. */
1277 /* Offset in prolog = 0. */
1280 switch (unw
[1] & 0xf)
1282 case UWOP_ALLOC_LARGE
:
1283 /* Expect < 512KB. */
1284 if ((unw
[1] & 0xf0) != 0)
1286 rsp
+= *(unsigned short *)(unw
+ 2) * 8;
1290 case UWOP_SAVE_NONVOL
:
1291 case UWOP_SAVE_XMM128
:
1295 case UWOP_PUSH_MACHFRAME
:
1298 rip
= (ULONG64
*)rsp
;
1299 if ((unw
[1] & 0xf0) == 0x10)
1314 EXCEPTION_DISPOSITION
1315 __gnat_personality_seh0 (PEXCEPTION_RECORD ms_exc
, void *this_frame
,
1316 PCONTEXT ms_orig_context
,
1317 PDISPATCHER_CONTEXT ms_disp
)
1319 /* Possibly transform run-time errors into Ada exceptions. As a small
1320 optimization, we call __gnat_SEH_error_handler only on non-user
1322 if (!(ms_exc
->ExceptionCode
& STATUS_USER_DEFINED
))
1324 struct Exception_Data
*exception
;
1326 ULONG64 excpip
= (ULONG64
) ms_exc
->ExceptionAddress
;
1329 && excpip
>= (ms_disp
->ImageBase
1330 + ms_disp
->FunctionEntry
->BeginAddress
)
1331 && excpip
< (ms_disp
->ImageBase
1332 + ms_disp
->FunctionEntry
->EndAddress
))
1334 /* This is a fault in this function. We need to adjust the return
1335 address before raising the GCC exception. */
1337 PRUNTIME_FUNCTION mf_func
= NULL
;
1338 ULONG64 mf_imagebase
;
1341 /* Get the context. */
1342 RtlCaptureContext (&context
);
1346 PRUNTIME_FUNCTION RuntimeFunction
;
1349 ULONG64 EstablisherFrame
;
1351 /* Get function metadata. */
1352 RuntimeFunction
= RtlLookupFunctionEntry
1353 (context
.Rip
, &ImageBase
, ms_disp
->HistoryTable
);
1354 if (RuntimeFunction
== ms_disp
->FunctionEntry
)
1356 mf_func
= RuntimeFunction
;
1357 mf_imagebase
= ImageBase
;
1358 mf_rsp
= context
.Rsp
;
1360 if (!RuntimeFunction
)
1362 /* In case of failure, assume this is a leaf function. */
1363 context
.Rip
= *(ULONG64
*) context
.Rsp
;
1369 RtlVirtualUnwind (0, ImageBase
, context
.Rip
, RuntimeFunction
,
1370 &context
, &HandlerData
, &EstablisherFrame
,
1374 /* 0 means bottom of the stack. */
1375 if (context
.Rip
== 0)
1381 if (mf_func
!= NULL
)
1382 __gnat_adjust_context
1383 ((unsigned char *)(mf_imagebase
+ mf_func
->UnwindData
), mf_rsp
);
1386 exception
= __gnat_map_SEH (ms_exc
, &msg
);
1387 if (exception
!= NULL
)
1389 struct _Unwind_Exception
*exc
;
1391 /* Directly convert the system exception to a GCC one.
1392 This is really breaking the API, but is necessary for stack size
1393 reasons: the normal way is to call Raise_From_Signal_Handler,
1394 which build the exception and calls _Unwind_RaiseException, which
1395 unwinds the stack and will call this personality routine. But
1396 the Windows unwinder needs about 2KB of stack. */
1397 exc
= __gnat_create_machine_occurrence_from_signal_handler
1399 memset (exc
->private_
, 0, sizeof (exc
->private_
));
1400 ms_exc
->ExceptionCode
= STATUS_GCC_THROW
;
1401 ms_exc
->NumberParameters
= 1;
1402 ms_exc
->ExceptionInformation
[0] = (ULONG_PTR
)exc
;
1407 return _GCC_specific_handler (ms_exc
, this_frame
, ms_orig_context
,
1408 ms_disp
, __gnat_personality_imp
);