re PR fortran/78659 ([F03] Spurious "requires DTIO" reported against namelist statement)
[official-gcc.git] / gcc / ada / raise-gcc.c
bloba2b6f645db6db97c5a2c895bcb20d04c3aaef6d6
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-2016, 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 #error "RTS unit only"
37 #endif
39 #ifndef CERT
40 #include "tconfig.h"
41 #include "tsystem.h"
42 #else
43 #define ATTRIBUTE_UNUSED __attribute__((unused))
44 #define HAVE_GETIPINFO 1
45 #endif
47 #include <stdarg.h>
48 typedef char bool;
49 # define true 1
50 # define false 0
52 #include "raise.h"
54 #ifdef __APPLE__
55 /* On MacOS X, versions older than 10.5 don't export _Unwind_GetIPInfo. */
56 #undef HAVE_GETIPINFO
57 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
58 #define HAVE_GETIPINFO 1
59 #endif
60 #endif
62 #if defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
63 /* HP-UX B.11.31 ia64 libunwind doesn't have _Unwind_GetIPInfo. */
64 #undef HAVE_GETIPINFO
65 #define _UA_END_OF_STACK 0
66 #endif
68 /* The names of a couple of "standard" routines for unwinding/propagation
69 actually vary depending on the underlying GCC scheme for exception handling
70 (SJLJ or DWARF). We need a consistently named interface to import from
71 a-except, so wrappers are defined here. */
73 #include "unwind.h"
75 typedef struct _Unwind_Context _Unwind_Context;
76 typedef struct _Unwind_Exception _Unwind_Exception;
78 _Unwind_Reason_Code
79 __gnat_Unwind_RaiseException (_Unwind_Exception *);
81 _Unwind_Reason_Code
82 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *, void *, void *);
84 extern struct Exception_Occurrence *__gnat_setup_current_excep
85 (_Unwind_Exception *);
86 extern void __gnat_unhandled_except_handler (_Unwind_Exception *);
88 #ifdef CERT
89 #define abort() __gnat_raise_abort()
90 static void __gnat_raise_abort(void)
92 while (1)
95 #endif
97 #include "unwind-pe.h"
99 /* The known and handled exception classes. */
101 #ifdef __ARM_EABI_UNWINDER__
102 #define CXX_EXCEPTION_CLASS "GNUCC++"
103 #define GNAT_EXCEPTION_CLASS "GNU-Ada"
104 #else
105 #define CXX_EXCEPTION_CLASS 0x474e5543432b2b00ULL
106 #define GNAT_EXCEPTION_CLASS 0x474e552d41646100ULL
107 #endif
109 /* Structure of a C++ exception, represented as a C structure... See
110 unwind-cxx.h for the full definition. */
112 struct __cxa_exception
114 void *exceptionType;
115 void (*exceptionDestructor)(void *);
117 void (*unexpectedHandler)();
118 void (*terminateHandler)();
120 struct __cxa_exception *nextException;
122 int handlerCount;
124 #ifdef __ARM_EABI_UNWINDER__
125 struct __cxa_exception* nextPropagatingException;
127 int propagationCount;
128 #else
129 int handlerSwitchValue;
130 const unsigned char *actionRecord;
131 const unsigned char *languageSpecificData;
132 _Unwind_Ptr catchTemp;
133 void *adjustedPtr;
134 #endif
136 _Unwind_Exception unwindHeader;
139 /* --------------------------------------------------------------
140 -- The DB stuff below is there for debugging purposes only. --
141 -------------------------------------------------------------- */
143 #ifndef inhibit_libc
145 #define DB_PHASES 0x1
146 #define DB_CSITE 0x2
147 #define DB_ACTIONS 0x4
148 #define DB_REGIONS 0x8
150 #define DB_ERR 0x1000
152 /* The "action" stuff below is also there for debugging purposes only. */
154 typedef struct
156 _Unwind_Action phase;
157 const char * description;
158 } phase_descriptor;
160 static const phase_descriptor phase_descriptors[]
161 = {{ _UA_SEARCH_PHASE, "SEARCH_PHASE" },
162 { _UA_CLEANUP_PHASE, "CLEANUP_PHASE" },
163 { _UA_HANDLER_FRAME, "HANDLER_FRAME" },
164 { _UA_FORCE_UNWIND, "FORCE_UNWIND" },
165 { -1, 0}};
167 static int
168 db_accepted_codes (void)
170 static int accepted_codes = -1;
172 if (accepted_codes == -1)
174 char * db_env = (char *) getenv ("EH_DEBUG");
176 accepted_codes = db_env ? (atoi (db_env) | DB_ERR) : 0;
177 /* Arranged for ERR stuff to always be visible when the variable
178 is defined. One may just set the variable to 0 to see the ERR
179 stuff only. */
182 return accepted_codes;
185 #define DB_INDENT_INCREASE 0x01
186 #define DB_INDENT_DECREASE 0x02
187 #define DB_INDENT_OUTPUT 0x04
188 #define DB_INDENT_NEWLINE 0x08
189 #define DB_INDENT_RESET 0x10
191 #define DB_INDENT_UNIT 8
193 static void
194 db_indent (int requests)
196 static int current_indentation_level = 0;
198 if (requests & DB_INDENT_RESET)
199 current_indentation_level = 0;
201 if (requests & DB_INDENT_INCREASE)
202 current_indentation_level ++;
204 if (requests & DB_INDENT_DECREASE)
205 current_indentation_level --;
207 if (requests & DB_INDENT_NEWLINE)
208 fprintf (stderr, "\n");
210 if (requests & DB_INDENT_OUTPUT)
211 fprintf (stderr, "%*s", current_indentation_level * DB_INDENT_UNIT, " ");
214 static void ATTRIBUTE_PRINTF_2
215 db (int db_code, char * msg_format, ...)
217 if (db_accepted_codes () & db_code)
219 va_list msg_args;
221 db_indent (DB_INDENT_OUTPUT);
223 va_start (msg_args, msg_format);
224 vfprintf (stderr, msg_format, msg_args);
225 va_end (msg_args);
229 static void
230 db_phases (int phases)
232 const phase_descriptor *a = phase_descriptors;
234 if (! (db_accepted_codes () & DB_PHASES))
235 return;
237 db (DB_PHASES, "\n");
239 for (; a->description != 0; a++)
240 if (phases & a->phase)
241 db (DB_PHASES, "%s ", a->description);
243 db (DB_PHASES, " :\n");
245 #else /* !inhibit_libc */
246 #define db_phases(X)
247 #define db_indent(X)
248 #define db(X, ...)
249 #endif /* !inhibit_libc */
251 /* ---------------------------------------------------------------
252 -- Now come a set of useful structures and helper routines. --
253 --------------------------------------------------------------- */
255 /* There are three major runtime tables involved, generated by the
256 GCC back-end. Contents slightly vary depending on the underlying
257 implementation scheme (dwarf zero cost / sjlj).
259 =======================================
260 * Tables for the dwarf zero cost case *
261 =======================================
263 They are fully documented in:
264 http://sourcery.mentor.com/public/cxx-abi/exceptions.pdf
265 Here is a shorter presentation, with some specific comments for Ada.
267 call_site []
268 -------------------------------------------------------------------
269 * region-start | region-length | landing-pad | first-action-index *
270 -------------------------------------------------------------------
272 Identify possible actions to be taken and where to resume control
273 for that when an exception propagates through a pc inside the region
274 delimited by start and length.
276 A null landing-pad indicates that nothing is to be done.
278 Otherwise, first-action-index provides an entry into the action[]
279 table which heads a list of possible actions to be taken (see below).
281 If it is determined that indeed an action should be taken, that
282 is, if one action filter matches the exception being propagated,
283 then control should be transferred to landing-pad.
285 A null first-action-index indicates that there are only cleanups
286 to run there.
288 action []
289 -------------------------------
290 * action-filter | next-action *
291 -------------------------------
293 This table contains lists (called action chains) of possible actions
294 associated with call-site entries described in the call-site [] table.
295 There is at most one action list per call-site entry. It is SLEB128
296 encoded.
298 A null action-filter indicates a cleanup.
300 Non null action-filters provide an index into the ttypes [] table
301 (see below), from which information may be retrieved to check if it
302 matches the exception being propagated.
304 * action-filter > 0:
305 means there is a regular handler to be run The value is also passed
306 to the landing pad to dispatch the exception.
308 * action-filter < 0:
309 means there is a some "exception_specification" data to retrieve,
310 which is only relevant for C++ and should never show up for Ada.
311 (Exception specification specifies which exceptions can be thrown
312 by a function. Such filter is emitted around the body of C++
313 functions defined like:
314 void foo ([...]) throw (A, B) { [...] }
315 These can be viewed as negativ filter: the landing pad is branched
316 to for exceptions that doesn't match the filter and usually aborts
317 the program).
319 * next-action
320 points to the next entry in the list using a relative byte offset. 0
321 indicates there is no other entry.
323 ttypes []
324 ---------------
325 * ttype-value *
326 ---------------
328 This table is an array of addresses.
330 A null value indicates a catch-all handler. (Not used by Ada)
332 Non null values are used to match the exception being propagated:
333 In C++ this is a pointer to some rtti data, while in Ada this is an
334 exception id (with a fake id for others).
336 For C++, this table is actually also used to store "exception
337 specification" data. The differentiation between the two kinds
338 of entries is made by the sign of the associated action filter,
339 which translates into positive or negative offsets from the
340 so called base of the table:
342 Exception Specification data is stored at positive offsets from
343 the ttypes table base, which Exception Type data is stored at
344 negative offsets:
346 ---------------------------------------------------------------------------
348 Here is a quick summary of the tables organization:
350 +-- Unwind_Context (pc, ...)
352 |(pc)
354 | CALL-SITE[]
356 | +=============================================================+
357 | | region-start + length | landing-pad | first-action-index |
358 | +=============================================================+
359 +-> | pc range 0 => no-action 0 => cleanups only |
360 | !0 => jump @ N --+ |
361 +====================================================== | ====+
364 ACTION [] |
366 +==========================================================+ |
367 | action-filter | next-action | |
368 +==========================================================+ |
369 | 0 => cleanup | |
370 | >0 => ttype index for handler ------+ 0 => end of chain | <-+
371 | <0 => ttype index for spec data | |
372 +==================================== | ===================+
375 TTYPES [] |
376 | Offset negated from
377 +=====================+ | the actual base.
378 | ttype-value | |
379 +============+=====================+ |
380 | | ... | |
381 | ... | exception id | <---+
382 | | ... |
383 | handlers +---------------------+
384 | | ... |
385 | ... | ... |
386 | | ... |
387 +============+=====================+ <<------ Table base
388 | ... | ... |
389 | specs | ... | (should not see negative filter
390 | ... | ... | values for Ada).
391 +============+=====================+
394 ============================
395 * Tables for the sjlj case *
396 ============================
398 So called "function contexts" are pushed on a context stack by calls to
399 _Unwind_SjLj_Register on function entry, and popped off at exit points by
400 calls to _Unwind_SjLj_Unregister. The current call_site for a function is
401 updated in the function context as the function's code runs along.
403 The generic unwinding engine in _Unwind_RaiseException walks the function
404 context stack and not the actual call chain.
406 The ACTION and TTYPES tables remain unchanged, which allows to search them
407 during the propagation phase to determine whether or not the propagated
408 exception is handled somewhere. When it is, we only "jump" up once directly
409 to the context where the handler will be found. Besides, this allows "break
410 exception unhandled" to work also
412 The CALL-SITE table is setup differently, though: the pc attached to the
413 unwind context is a direct index into the table, so the entries in this
414 table do not hold region bounds any more.
416 A special index (-1) is used to indicate that no action is possibly
417 connected with the context at hand, so null landing pads cannot appear
418 in the table.
420 Additionally, landing pad values in the table do not represent code address
421 to jump at, but so called "dispatch" indices used by a common landing pad
422 for the function to switch to the appropriate post-landing-pad.
424 +-- Unwind_Context (pc, ...)
426 | pc = call-site index
427 | 0 => terminate (should not see this for Ada)
428 | -1 => no-action
430 | CALL-SITE[]
432 | +=====================================+
433 | | landing-pad | first-action-index |
434 | +=====================================+
435 +-> | 0 => cleanups only |
436 | dispatch index N |
437 +=====================================+
440 ===================================
441 * Basic organization of this unit *
442 ===================================
444 The major point of this unit is to provide an exception propagation
445 personality routine for Ada. This is __gnat_personality_v0.
447 It is provided with a pointer to the propagated exception, an unwind
448 context describing a location the propagation is going through, and a
449 couple of other arguments including a description of the current
450 propagation phase.
452 It shall return to the generic propagation engine what is to be performed
453 next, after possible context adjustments, depending on what it finds in the
454 traversed context (a handler for the exception, a cleanup, nothing, ...),
455 and on the propagation phase.
457 A number of structures and subroutines are used for this purpose, as
458 sketched below:
460 o region_descriptor: General data associated with the context (base pc,
461 call-site table, action table, ttypes table, ...)
463 o action_descriptor: Data describing the action to be taken for the
464 propagated exception in the provided context (kind of action: nothing,
465 handler, cleanup; pointer to the action table entry, ...).
467 raise
469 ... (a-except.adb)
471 Propagate_Exception (a-exexpr.adb)
474 _Unwind_RaiseException (libgcc)
476 | (Ada frame)
478 +--> __gnat_personality_v0 (context, exception)
480 +--> get_region_description_for (context)
482 +--> get_action_description_for (ip, exception, region)
484 | +--> get_call_site_action_for (context, region)
485 | (one version for each underlying scheme)
487 +--> setup_to_install (context)
489 This unit is inspired from the C++ version found in eh_personality.cc,
490 part of libstdc++-v3.
495 /* This is an incomplete "proxy" of the structure of exception objects as
496 built by the GNAT runtime library. Accesses to other fields than the common
497 header are performed through subprogram calls to alleviate the need of an
498 exact counterpart here and potential alignment/size issues for the common
499 header. See a-exexpr.adb. */
501 typedef struct
503 _Unwind_Exception common;
504 /* ABI header, maximally aligned. */
505 } _GNAT_Exception;
507 /* The two constants below are specific ttype identifiers for special
508 exception ids. Their type should match what a-exexpr exports. */
510 extern const int __gnat_others_value;
511 #define GNAT_OTHERS ((_Unwind_Ptr) &__gnat_others_value)
513 extern const int __gnat_all_others_value;
514 #define GNAT_ALL_OTHERS ((_Unwind_Ptr) &__gnat_all_others_value)
516 extern const int __gnat_unhandled_others_value;
517 #define GNAT_UNHANDLED_OTHERS ((_Unwind_Ptr) &__gnat_unhandled_others_value)
519 /* Describe the useful region data associated with an unwind context. */
521 typedef struct
523 /* The base pc of the region. */
524 _Unwind_Ptr base;
526 /* Pointer to the Language Specific Data for the region. */
527 _Unwind_Ptr lsda;
529 /* Call-Site data associated with this region. */
530 unsigned char call_site_encoding;
531 const unsigned char *call_site_table;
533 /* The base to which are relative landing pad offsets inside the call-site
534 entries . */
535 _Unwind_Ptr lp_base;
537 /* Action-Table associated with this region. */
538 const unsigned char *action_table;
540 /* Ttype data associated with this region. */
541 unsigned char ttype_encoding;
542 const unsigned char *ttype_table;
543 _Unwind_Ptr ttype_base;
545 } region_descriptor;
547 /* Extract and adjust the IP (instruction pointer) from an exception
548 context. */
550 static _Unwind_Ptr
551 get_ip_from_context (_Unwind_Context *uw_context)
553 int ip_before_insn = 0;
554 #ifdef HAVE_GETIPINFO
555 _Unwind_Ptr ip = _Unwind_GetIPInfo (uw_context, &ip_before_insn);
556 #else
557 _Unwind_Ptr ip = _Unwind_GetIP (uw_context);
558 #endif
559 /* Subtract 1 if necessary because GetIPInfo yields a call return address
560 in this case, while we are interested in information for the call point.
561 This does not always yield the exact call instruction address but always
562 brings the IP back within the corresponding region. */
563 if (!ip_before_insn)
564 ip--;
566 return ip;
569 static void
570 db_region_for (region_descriptor *region, _Unwind_Ptr ip)
572 #ifndef inhibit_libc
573 if (! (db_accepted_codes () & DB_REGIONS))
574 return;
576 db (DB_REGIONS, "For ip @ %p => ", (void *)ip);
578 if (region->lsda)
579 db (DB_REGIONS, "lsda @ %p", (void *)region->lsda);
580 else
581 db (DB_REGIONS, "no lsda");
583 db (DB_REGIONS, "\n");
584 #endif
587 /* Retrieve the ttype entry associated with FILTER in the REGION's
588 ttype table. */
590 static _Unwind_Ptr
591 get_ttype_entry_for (region_descriptor *region, long filter)
593 _Unwind_Ptr ttype_entry;
595 filter *= size_of_encoded_value (region->ttype_encoding);
596 read_encoded_value_with_base
597 (region->ttype_encoding, region->ttype_base,
598 region->ttype_table - filter, &ttype_entry);
600 return ttype_entry;
603 /* Fill out the REGION descriptor for the provided UW_CONTEXT. */
605 static void
606 get_region_description_for (_Unwind_Context *uw_context,
607 region_descriptor *region)
609 const unsigned char * p;
610 _uleb128_t tmp;
611 unsigned char lpbase_encoding;
613 /* Get the base address of the lsda information. If the provided context
614 is null or if there is no associated language specific data, there's
615 nothing we can/should do. */
616 region->lsda
617 = (_Unwind_Ptr) (uw_context
618 ? _Unwind_GetLanguageSpecificData (uw_context) : 0);
620 if (! region->lsda)
621 return;
623 /* Parse the lsda and fill the region descriptor. */
624 p = (const unsigned char *)region->lsda;
626 region->base = _Unwind_GetRegionStart (uw_context);
628 /* Find @LPStart, the base to which landing pad offsets are relative. */
629 lpbase_encoding = *p++;
630 if (lpbase_encoding != DW_EH_PE_omit)
631 p = read_encoded_value
632 (uw_context, lpbase_encoding, p, &region->lp_base);
633 else
634 region->lp_base = region->base;
636 /* Find @TType, the base of the handler and exception spec type data. */
637 region->ttype_encoding = *p++;
638 if (region->ttype_encoding != DW_EH_PE_omit)
640 p = read_uleb128 (p, &tmp);
641 region->ttype_table = p + tmp;
643 else
644 region->ttype_table = 0;
646 region->ttype_base
647 = base_of_encoded_value (region->ttype_encoding, uw_context);
649 /* Get the encoding and length of the call-site table; the action table
650 immediately follows. */
651 region->call_site_encoding = *p++;
652 region->call_site_table = read_uleb128 (p, &tmp);
654 region->action_table = region->call_site_table + tmp;
658 /* Describe an action to be taken when propagating an exception up to
659 some context. */
661 enum action_kind
663 /* Found some call site base data, but need to analyze further
664 before being able to decide. */
665 unknown,
667 /* There is nothing relevant in the context at hand. */
668 nothing,
670 /* There are only cleanups to run in this context. */
671 cleanup,
673 /* There is a handler for the exception in this context. */
674 handler,
676 /* There is a handler for the exception, but it is only for catching
677 unhandled exceptions. */
678 unhandler
681 /* filter value for cleanup actions. */
682 static const int cleanup_filter = 0;
684 typedef struct
686 /* The kind of action to be taken. */
687 enum action_kind kind;
689 /* A pointer to the action record entry. */
690 const unsigned char *table_entry;
692 /* Where we should jump to actually take an action (trigger a cleanup or an
693 exception handler). */
694 _Unwind_Ptr landing_pad;
696 /* If we have a handler matching our exception, these are the filter to
697 trigger it and the corresponding id. */
698 _Unwind_Sword ttype_filter;
700 } action_descriptor;
702 static void
703 db_action_for (action_descriptor *action, _Unwind_Ptr ip)
705 #ifndef inhibit_libc
706 db (DB_ACTIONS, "For ip @ %p => ", (void *)ip);
708 switch (action->kind)
710 case unknown:
711 db (DB_ACTIONS, "lpad @ %p, record @ %p\n",
712 (void *) action->landing_pad, action->table_entry);
713 break;
715 case nothing:
716 db (DB_ACTIONS, "Nothing\n");
717 break;
719 case cleanup:
720 db (DB_ACTIONS, "Cleanup\n");
721 break;
723 case handler:
724 db (DB_ACTIONS, "Handler, filter = %d\n", (int) action->ttype_filter);
725 break;
727 default:
728 db (DB_ACTIONS, "Err? Unexpected action kind !\n");
729 break;
731 #endif
734 /* Search the call_site_table of REGION for an entry appropriate for the
735 UW_CONTEXT's IP. If one is found, store the associated landing_pad
736 and action_table entry, and set the ACTION kind to unknown for further
737 analysis. Otherwise, set the ACTION kind to nothing.
739 There are two variants of this routine, depending on the underlying
740 mechanism (DWARF/SJLJ), which account for differences in the tables. */
742 #ifdef __USING_SJLJ_EXCEPTIONS__
744 #define __builtin_eh_return_data_regno(x) x
746 static void
747 get_call_site_action_for (_Unwind_Ptr call_site,
748 region_descriptor *region,
749 action_descriptor *action)
751 /* call_site is a direct index into the call-site table, with two special
752 values : -1 for no-action and 0 for "terminate". The latter should never
753 show up for Ada. To test for the former, beware that _Unwind_Ptr might
754 be unsigned. */
756 if ((int)call_site < 0)
758 action->kind = nothing;
760 else if (call_site == 0)
762 db (DB_ERR, "========> Err, null call_site for Ada/sjlj\n");
763 action->kind = nothing;
765 else
767 _uleb128_t cs_lp, cs_action;
768 const unsigned char *p;
770 /* Let the caller know there may be an action to take, but let it
771 determine the kind. */
772 action->kind = unknown;
774 /* We have a direct index into the call-site table, but this table is
775 made of leb128 values, the encoding length of which is variable. We
776 can't merely compute an offset from the index, then, but have to read
777 all the entries before the one of interest. */
778 p = region->call_site_table;
781 p = read_uleb128 (p, &cs_lp);
782 p = read_uleb128 (p, &cs_action);
784 while (--call_site);
786 action->landing_pad = cs_lp + 1;
788 if (cs_action)
789 action->table_entry = region->action_table + cs_action - 1;
790 else
791 action->table_entry = 0;
795 #else /* !__USING_SJLJ_EXCEPTIONS__ */
797 static void
798 get_call_site_action_for (_Unwind_Ptr ip,
799 region_descriptor *region,
800 action_descriptor *action)
802 const unsigned char *p = region->call_site_table;
804 /* Unless we are able to determine otherwise... */
805 action->kind = nothing;
807 db (DB_CSITE, "\n");
809 while (p < region->action_table)
811 _Unwind_Ptr cs_start, cs_len, cs_lp;
812 _uleb128_t cs_action;
814 /* Note that all call-site encodings are "absolute" displacements. */
815 p = read_encoded_value (0, region->call_site_encoding, p, &cs_start);
816 p = read_encoded_value (0, region->call_site_encoding, p, &cs_len);
817 p = read_encoded_value (0, region->call_site_encoding, p, &cs_lp);
818 p = read_uleb128 (p, &cs_action);
820 db (DB_CSITE,
821 "c_site @ %p (+%p), len = %p, lpad @ %p (+%p)\n",
822 (void *)region->base + cs_start, (void *)cs_start, (void *)cs_len,
823 (void *)region->lp_base + cs_lp, (void *)cs_lp);
825 /* The table is sorted, so if we've passed the IP, stop. */
826 if (ip < region->base + cs_start)
827 break;
829 /* If we have a match, fill the ACTION fields accordingly. */
830 else if (ip < region->base + cs_start + cs_len)
832 /* Let the caller know there may be an action to take, but let it
833 determine the kind. */
834 action->kind = unknown;
836 if (cs_lp)
837 action->landing_pad = region->lp_base + cs_lp;
838 else
839 action->landing_pad = 0;
841 if (cs_action)
842 action->table_entry = region->action_table + cs_action - 1;
843 else
844 action->table_entry = 0;
846 db (DB_CSITE, "+++\n");
847 return;
851 db (DB_CSITE, "---\n");
854 #endif /* __USING_SJLJ_EXCEPTIONS__ */
856 /* With CHOICE an exception choice representing an "exception - when"
857 argument, and PROPAGATED_EXCEPTION a pointer to the currently propagated
858 occurrence, return true if the latter matches the former, that is, if
859 PROPAGATED_EXCEPTION is caught by the handling code controlled by CHOICE.
860 This takes care of the special Non_Ada_Error case on VMS. */
862 #define Is_Handled_By_Others __gnat_is_handled_by_others
863 #define Language_For __gnat_language_for
864 #define Foreign_Data_For __gnat_foreign_data_for
865 #define EID_For __gnat_eid_for
867 extern bool Is_Handled_By_Others (_Unwind_Ptr eid);
868 extern char Language_For (_Unwind_Ptr eid);
870 extern void *Foreign_Data_For (_Unwind_Ptr eid);
872 extern Exception_Id EID_For (_GNAT_Exception * e);
874 #define Foreign_Exception system__exceptions__foreign_exception
875 extern struct Exception_Data Foreign_Exception;
877 #ifdef VMS
878 #define Non_Ada_Error system__aux_dec__non_ada_error
879 extern struct Exception_Data Non_Ada_Error;
880 #endif
882 /* Return true iff the exception class of EXCEPT is EC. */
884 static int
885 exception_class_eq (const _GNAT_Exception *except, _Unwind_Exception_Class ec)
887 #ifdef __ARM_EABI_UNWINDER__
888 return memcmp (except->common.exception_class, ec, 8) == 0;
889 #else
890 return except->common.exception_class == ec;
891 #endif
894 /* Return how CHOICE matches PROPAGATED_EXCEPTION. */
896 static enum action_kind
897 is_handled_by (_Unwind_Ptr choice, _GNAT_Exception *propagated_exception)
899 /* All others choice match everything. */
900 if (choice == GNAT_ALL_OTHERS)
901 return handler;
903 /* GNAT exception occurrence. */
904 if (exception_class_eq (propagated_exception, GNAT_EXCEPTION_CLASS))
906 /* Pointer to the GNAT exception data corresponding to the propagated
907 occurrence. */
908 _Unwind_Ptr E = (_Unwind_Ptr) EID_For (propagated_exception);
910 if (choice == GNAT_UNHANDLED_OTHERS)
911 return unhandler;
913 E = (_Unwind_Ptr) EID_For (propagated_exception);
915 /* Base matching rules: An exception data (id) matches itself, "when
916 all_others" matches anything and "when others" matches anything
917 unless explicitly stated otherwise in the propagated occurrence. */
918 if (choice == E || (choice == GNAT_OTHERS && Is_Handled_By_Others (E)))
919 return handler;
921 #ifdef VMS
922 /* In addition, on OpenVMS, Non_Ada_Error matches VMS exceptions, and we
923 may have different exception data pointers that should match for the
924 same condition code, if both an export and an import have been
925 registered. The import code for both the choice and the propagated
926 occurrence are expected to have been masked off regarding severity
927 bits already (at registration time for the former and from within the
928 low level exception vector for the latter). */
929 if ((Language_For (E) == 'V'
930 && choice != GNAT_OTHERS
931 && ((Language_For (choice) == 'V'
932 && Foreign_Data_For (choice) != 0
933 && Foreign_Data_For (choice) == Foreign_Data_For (E))
934 || choice == (_Unwind_Ptr)&Non_Ada_Error)))
935 return handler;
936 #endif
938 /* Otherwise, it doesn't match an Ada choice. */
939 return nothing;
942 /* All others and others choice match any foreign exception. */
943 if (choice == GNAT_ALL_OTHERS
944 || choice == GNAT_OTHERS
945 #ifndef CERT
946 || choice == (_Unwind_Ptr) &Foreign_Exception
947 #endif
949 return handler;
951 #ifndef CERT
952 /* C++ exception occurrences. */
953 if (exception_class_eq (propagated_exception, CXX_EXCEPTION_CLASS)
954 && Language_For (choice) == 'C')
956 void *choice_typeinfo = Foreign_Data_For (choice);
957 void *except_typeinfo =
958 (((struct __cxa_exception *)
959 ((_Unwind_Exception *)propagated_exception + 1)) - 1)
960 ->exceptionType;
962 /* Typeinfo are directly compared, which might not be correct if they
963 aren't merged. ??? We should call the == operator if this module is
964 compiled in C++. */
965 if (choice_typeinfo == except_typeinfo)
966 return handler;
968 #endif
970 return nothing;
973 /* Fill out the ACTION to be taken from propagating UW_EXCEPTION up to
974 UW_CONTEXT in REGION. */
976 static void
977 get_action_description_for (_Unwind_Ptr ip,
978 _Unwind_Exception *uw_exception,
979 _Unwind_Action uw_phase,
980 region_descriptor *region,
981 action_descriptor *action)
983 _GNAT_Exception *gnat_exception = (_GNAT_Exception *) uw_exception;
985 /* Search the call site table first, which may get us a landing pad as well
986 as the head of an action record list. */
987 get_call_site_action_for (ip, region, action);
988 db_action_for (action, ip);
990 /* If there is not even a call_site entry, we are done. */
991 if (action->kind == nothing)
992 return;
994 /* Otherwise, check what we have at the place of the call site. */
996 /* No landing pad => no cleanups or handlers. */
997 if (action->landing_pad == 0)
999 action->kind = nothing;
1000 return;
1003 /* Landing pad + null table entry => only cleanups. */
1004 else if (action->table_entry == 0)
1006 action->kind = cleanup;
1007 action->ttype_filter = cleanup_filter;
1008 /* The filter initialization is not strictly necessary, as cleanup-only
1009 landing pads don't look at the filter value. It is there to ensure
1010 we don't pass random values and so trigger potential confusion when
1011 installing the context later on. */
1012 return;
1015 /* Landing pad + Table entry => handlers + possible cleanups. */
1016 else
1018 const unsigned char * p = action->table_entry;
1019 _sleb128_t ar_filter, ar_disp;
1021 action->kind = nothing;
1023 while (1)
1025 p = read_sleb128 (p, &ar_filter);
1026 read_sleb128 (p, &ar_disp);
1027 /* Don't assign p here, as it will be incremented by ar_disp
1028 below. */
1030 /* Null filters are for cleanups. */
1031 if (ar_filter == cleanup_filter)
1033 action->kind = cleanup;
1034 action->ttype_filter = cleanup_filter;
1035 /* The filter initialization is required here, to ensure
1036 the target landing pad branches to the cleanup code if
1037 we happen not to find a matching handler. */
1040 /* Positive filters are for regular handlers. */
1041 else if (ar_filter > 0)
1043 /* Do not catch an exception if the _UA_FORCE_UNWIND flag is
1044 passed (to follow the ABI). */
1045 if (!(uw_phase & _UA_FORCE_UNWIND))
1047 enum action_kind act;
1049 /* See if the filter we have is for an exception which
1050 matches the one we are propagating. */
1051 _Unwind_Ptr choice =
1052 get_ttype_entry_for (region, ar_filter);
1054 act = is_handled_by (choice, gnat_exception);
1055 if (act != nothing)
1057 action->kind = act;
1058 action->ttype_filter = ar_filter;
1059 return;
1064 /* Negative filter values are for C++ exception specifications.
1065 Should not be there for Ada :/ */
1066 else
1067 db (DB_ERR, "========> Err, filter < 0 for Ada/dwarf\n");
1069 if (ar_disp == 0)
1070 return;
1072 p += ar_disp;
1077 /* Setup in UW_CONTEXT the eh return target IP and data registers, which will
1078 be restored with the others and retrieved by the landing pad once the jump
1079 occurred. */
1081 static void
1082 setup_to_install (_Unwind_Context *uw_context,
1083 _Unwind_Exception *uw_exception,
1084 _Unwind_Ptr uw_landing_pad,
1085 int uw_filter)
1087 /* 1/ exception object pointer, which might be provided back to
1088 _Unwind_Resume (and thus to this personality routine) if we are jumping
1089 to a cleanup. */
1090 _Unwind_SetGR (uw_context, __builtin_eh_return_data_regno (0),
1091 (_Unwind_Word)uw_exception);
1093 /* 2/ handler switch value register, which will also be used by the target
1094 landing pad to decide what action it shall take. */
1095 _Unwind_SetGR (uw_context, __builtin_eh_return_data_regno (1),
1096 (_Unwind_Word)uw_filter);
1098 /* Setup the address we should jump at to reach the code where there is the
1099 "something" we found. */
1100 _Unwind_SetIP (uw_context, uw_landing_pad);
1103 /* The following is defined from a-except.adb. Its purpose is to enable
1104 automatic backtraces upon exception raise, as provided through the
1105 GNAT.Traceback facilities. */
1106 extern void __gnat_notify_handled_exception (struct Exception_Occurrence *);
1107 extern void __gnat_notify_unhandled_exception (struct Exception_Occurrence *);
1109 /* Below is the eh personality routine per se. We currently assume that only
1110 GNU-Ada exceptions are met. */
1112 /* By default, the personality routine is public. */
1113 #define PERSONALITY_STORAGE
1115 #ifdef __USING_SJLJ_EXCEPTIONS__
1116 #define PERSONALITY_FUNCTION __gnat_personality_sj0
1117 #elif defined (__SEH__)
1118 #define PERSONALITY_FUNCTION __gnat_personality_imp
1119 /* The public personality routine for seh is __gnat_personality_seh0, defined
1120 below using the SEH convention. This is a wrapper around the GNU routine,
1121 which is static. */
1122 #undef PERSONALITY_STORAGE
1123 #define PERSONALITY_STORAGE static
1124 #else
1125 #define PERSONALITY_FUNCTION __gnat_personality_v0
1126 #endif
1128 /* Code executed to continue unwinding. With the ARM unwinder, the
1129 personality routine must unwind one frame (per EHABI 7.3 4.). */
1131 static _Unwind_Reason_Code
1132 continue_unwind (struct _Unwind_Exception* ue_header ATTRIBUTE_UNUSED,
1133 struct _Unwind_Context* uw_context ATTRIBUTE_UNUSED)
1135 #ifdef __ARM_EABI_UNWINDER__
1136 if (__gnu_unwind_frame (ue_header, uw_context) != _URC_OK)
1137 return _URC_FAILURE;
1138 #endif
1139 return _URC_CONTINUE_UNWIND;
1142 /* Common code for the body of GNAT personality routine. This code is shared
1143 between all unwinders. */
1145 static _Unwind_Reason_Code
1146 personality_body (_Unwind_Action uw_phases,
1147 _Unwind_Exception *uw_exception,
1148 _Unwind_Context *uw_context)
1150 region_descriptor region;
1151 action_descriptor action;
1152 _Unwind_Ptr ip;
1154 /* Debug traces. */
1155 db_indent (DB_INDENT_RESET);
1156 db_phases (uw_phases);
1157 db_indent (DB_INDENT_INCREASE);
1159 /* Get the region description for the context we were provided with. This
1160 will tell us if there is some lsda, call_site, action and/or ttype data
1161 for the associated ip. */
1162 get_region_description_for (uw_context, &region);
1164 /* No LSDA => no handlers or cleanups => we shall unwind further up. */
1165 if (! region.lsda)
1166 return continue_unwind (uw_exception, uw_context);
1168 /* Get the instruction pointer. */
1169 ip = get_ip_from_context (uw_context);
1170 db_region_for (&region, ip);
1172 /* Search the call-site and action-record tables for the action associated
1173 with this IP. */
1174 get_action_description_for (ip, uw_exception, uw_phases, &region, &action);
1175 db_action_for (&action, ip);
1177 /* Whatever the phase, if there is nothing relevant in this frame,
1178 unwinding should just go on. */
1179 if (action.kind == nothing)
1180 return continue_unwind (uw_exception, uw_context);
1182 /* If we found something in search phase, we should return a code indicating
1183 what to do next depending on what we found. If we only have cleanups
1184 around, we shall try to unwind further up to find a handler, otherwise,
1185 tell we have a handler, which will trigger the second phase. */
1186 if (uw_phases & _UA_SEARCH_PHASE)
1188 if (action.kind == cleanup)
1190 return continue_unwind (uw_exception, uw_context);
1192 else
1194 #ifndef CERT
1195 struct Exception_Occurrence *excep;
1197 /* Trigger the appropriate notification routines before the second
1198 phase starts, which ensures the stack is still intact.
1199 First, setup the Ada occurrence. */
1200 excep = __gnat_setup_current_excep (uw_exception);
1201 if (action.kind == unhandler)
1202 __gnat_notify_unhandled_exception (excep);
1203 else
1204 __gnat_notify_handled_exception (excep);
1205 #endif
1207 return _URC_HANDLER_FOUND;
1211 /* We found something in cleanup/handler phase, which might be the handler
1212 or a cleanup for a handled occurrence, or a cleanup for an unhandled
1213 occurrence (we are in a FORCED_UNWIND phase in this case). Install the
1214 context to get there. */
1216 setup_to_install
1217 (uw_context, uw_exception, action.landing_pad, action.ttype_filter);
1219 #ifndef CERT
1220 /* Write current exception, so that it can be retrieved from Ada. It was
1221 already done during phase 1 (just above), but in between, one or several
1222 exceptions may have been raised (in cleanup handlers). */
1223 __gnat_setup_current_excep (uw_exception);
1224 #endif
1226 return _URC_INSTALL_CONTEXT;
1229 #ifndef __ARM_EABI_UNWINDER__
1230 /* Major tweak for ia64-vms : the CHF propagation phase calls this personality
1231 routine with sigargs/mechargs arguments and has very specific expectations
1232 on possible return values.
1234 We handle this with a number of specific tricks:
1236 1. We tweak the personality routine prototype to have the "version" and
1237 "phases" two first arguments be void * instead of int and _Unwind_Action
1238 as nominally expected in the GCC context.
1240 This allows us to access the full range of bits passed in every case and
1241 has no impact on the callers side since each argument remains assigned
1242 the same single 64bit slot.
1244 2. We retrieve the corresponding int and _Unwind_Action values within the
1245 routine for regular use with truncating conversions. This is a noop when
1246 called from the libgcc unwinder.
1248 3. We assume we're called by the VMS CHF when unexpected bits are set in
1249 both those values. The incoming arguments are then real sigargs and
1250 mechargs pointers, which we then redirect to __gnat_handle_vms_condition
1251 for proper processing.
1253 #if defined (VMS) && defined (__IA64)
1254 typedef void * version_arg_t;
1255 typedef void * phases_arg_t;
1256 #else
1257 typedef int version_arg_t;
1258 typedef _Unwind_Action phases_arg_t;
1259 #endif
1261 PERSONALITY_STORAGE _Unwind_Reason_Code
1262 PERSONALITY_FUNCTION (version_arg_t, phases_arg_t,
1263 _Unwind_Exception_Class, _Unwind_Exception *,
1264 _Unwind_Context *);
1266 PERSONALITY_STORAGE _Unwind_Reason_Code
1267 PERSONALITY_FUNCTION (version_arg_t version_arg,
1268 phases_arg_t phases_arg,
1269 _Unwind_Exception_Class uw_exception_class
1270 ATTRIBUTE_UNUSED,
1271 _Unwind_Exception *uw_exception,
1272 _Unwind_Context *uw_context)
1274 /* Fetch the version and phases args with their nominal ABI types for later
1275 use. This is a noop everywhere except on ia64-vms when called from the
1276 Condition Handling Facility. */
1277 int uw_version = (int) version_arg;
1278 _Unwind_Action uw_phases = (_Unwind_Action) phases_arg;
1280 /* Check that we're called from the ABI context we expect, with a major
1281 possible variation on VMS for IA64. */
1282 if (uw_version != 1)
1284 #if defined (VMS) && defined (__IA64)
1286 /* Assume we're called with sigargs/mechargs arguments if really
1287 unexpected bits are set in our first two formals. Redirect to the
1288 GNAT condition handling code in this case. */
1290 extern long __gnat_handle_vms_condition (void *, void *);
1292 unsigned int version_unexpected_bits_mask = 0xffffff00U;
1293 unsigned int phases_unexpected_bits_mask = 0xffffff00U;
1295 if ((unsigned int)uw_version & version_unexpected_bits_mask
1296 && (unsigned int)uw_phases & phases_unexpected_bits_mask)
1297 return __gnat_handle_vms_condition (version_arg, phases_arg);
1298 #endif
1300 return _URC_FATAL_PHASE1_ERROR;
1303 return personality_body (uw_phases, uw_exception, uw_context);
1306 #else /* __ARM_EABI_UNWINDER__ */
1308 PERSONALITY_STORAGE _Unwind_Reason_Code
1309 PERSONALITY_FUNCTION (_Unwind_State state,
1310 struct _Unwind_Exception* ue_header,
1311 struct _Unwind_Context* uw_context);
1313 PERSONALITY_STORAGE _Unwind_Reason_Code
1314 PERSONALITY_FUNCTION (_Unwind_State state,
1315 struct _Unwind_Exception* uw_exception,
1316 struct _Unwind_Context* uw_context)
1318 _Unwind_Action uw_phases;
1320 switch (state & _US_ACTION_MASK)
1322 case _US_VIRTUAL_UNWIND_FRAME:
1323 /* Phase 1. */
1324 uw_phases = _UA_SEARCH_PHASE;
1325 break;
1327 case _US_UNWIND_FRAME_STARTING:
1328 /* Phase 2, to call a cleanup. */
1329 uw_phases = _UA_CLEANUP_PHASE;
1330 #if 0
1331 /* ??? We don't use UA_HANDLER_FRAME (except to debug). Futhermore,
1332 barrier_cache.sp isn't yet set. */
1333 if (!(state & _US_FORCE_UNWIND)
1334 && (uw_exception->barrier_cache.sp
1335 == _Unwind_GetGR (uw_context, UNWIND_STACK_REG)))
1336 uw_phases |= _UA_HANDLER_FRAME;
1337 #endif
1338 break;
1340 case _US_UNWIND_FRAME_RESUME:
1341 /* Phase 2, called at the return of a cleanup. In the GNU
1342 implementation, there is nothing left to do, so we simply go on. */
1343 return continue_unwind (uw_exception, uw_context);
1345 default:
1346 return _URC_FAILURE;
1348 uw_phases |= (state & _US_FORCE_UNWIND);
1350 /* The dwarf unwinder assumes the context structure holds things like the
1351 function and LSDA pointers. The ARM implementation caches these in
1352 the exception header (UCB). To avoid rewriting everything we make a
1353 virtual scratch register point at the UCB. This is a GNU specific
1354 requirement. */
1355 _Unwind_SetGR (uw_context, UNWIND_POINTER_REG, (_Unwind_Ptr) uw_exception);
1357 return personality_body (uw_phases, uw_exception, uw_context);
1359 #endif /* __ARM_EABI_UNWINDER__ */
1361 /* Callback routine called by Unwind_ForcedUnwind to execute all the cleanup
1362 before exiting the task. */
1364 #ifndef CERT
1365 _Unwind_Reason_Code
1366 __gnat_cleanupunwind_handler (int version ATTRIBUTE_UNUSED,
1367 _Unwind_Action phases,
1368 _Unwind_Exception_Class eclass ATTRIBUTE_UNUSED,
1369 struct _Unwind_Exception *exception,
1370 struct _Unwind_Context *context ATTRIBUTE_UNUSED,
1371 void *arg ATTRIBUTE_UNUSED)
1373 /* Terminate when the end of the stack is reached. */
1374 if ((phases & _UA_END_OF_STACK) != 0
1375 #if defined (__ia64__) && defined (__hpux__) && defined (USE_LIBUNWIND_EXCEPTIONS)
1376 /* Strictely follow the ia64 ABI: when end of stack is reached,
1377 the callback will be called with a NULL stack pointer.
1378 No need for that when using libgcc unwinder. */
1379 || _Unwind_GetGR (context, 12) == 0
1380 #endif
1382 __gnat_unhandled_except_handler (exception);
1384 /* We know there is at least one cleanup further up. Return so that it
1385 is searched and entered, after which Unwind_Resume will be called
1386 and this hook will gain control again. */
1387 return _URC_NO_REASON;
1389 #endif
1391 /* Define the consistently named wrappers imported by Propagate_Exception. */
1393 _Unwind_Reason_Code
1394 __gnat_Unwind_RaiseException (_Unwind_Exception *e)
1396 #ifdef __USING_SJLJ_EXCEPTIONS__
1397 return _Unwind_SjLj_RaiseException (e);
1398 #else
1399 return _Unwind_RaiseException (e);
1400 #endif
1403 _Unwind_Reason_Code
1404 __gnat_Unwind_ForcedUnwind (_Unwind_Exception *e ATTRIBUTE_UNUSED,
1405 void *handler ATTRIBUTE_UNUSED,
1406 void *argument ATTRIBUTE_UNUSED)
1408 #ifdef __USING_SJLJ_EXCEPTIONS__
1410 # if defined (__APPLE__) && defined (__arm__)
1411 /* There is not ForcedUnwind routine in arm-darwin system library. */
1412 return _URC_FATAL_PHASE1_ERROR;
1413 # else
1414 return _Unwind_SjLj_ForcedUnwind (e, handler, argument);
1415 # endif
1417 #else
1418 return _Unwind_ForcedUnwind (e, handler, argument);
1419 #endif
1422 #if defined (__SEH__) && !defined (__USING_SJLJ_EXCEPTIONS__)
1424 #define STATUS_USER_DEFINED (1U << 29)
1426 /* From unwind-seh.c. */
1427 #define GCC_MAGIC (('G' << 16) | ('C' << 8) | 'C')
1428 #define GCC_EXCEPTION(TYPE) \
1429 (STATUS_USER_DEFINED | ((TYPE) << 24) | GCC_MAGIC)
1430 #define STATUS_GCC_THROW GCC_EXCEPTION (0)
1432 EXCEPTION_DISPOSITION __gnat_SEH_error_handler
1433 (struct _EXCEPTION_RECORD*, void*, struct _CONTEXT*, void*);
1435 struct Exception_Data *
1436 __gnat_map_SEH (EXCEPTION_RECORD* ExceptionRecord, const char **msg);
1438 struct _Unwind_Exception *
1439 __gnat_create_machine_occurrence_from_signal_handler (Exception_Id,
1440 const char *);
1442 /* Unwind opcodes. */
1443 #define UWOP_PUSH_NONVOL 0
1444 #define UWOP_ALLOC_LARGE 1
1445 #define UWOP_ALLOC_SMALL 2
1446 #define UWOP_SET_FPREG 3
1447 #define UWOP_SAVE_NONVOL 4
1448 #define UWOP_SAVE_NONVOL_FAR 5
1449 #define UWOP_SAVE_XMM128 8
1450 #define UWOP_SAVE_XMM128_FAR 9
1451 #define UWOP_PUSH_MACHFRAME 10
1453 /* Modify the IP value saved in the machine frame. This is really a kludge,
1454 that will be removed if we could propagate the Windows exception (and not
1455 the GCC one).
1456 What is very wrong is that the Windows unwinder will try to decode the
1457 instruction at IP, which isn't valid anymore after the adjust. */
1459 static void
1460 __gnat_adjust_context (unsigned char *unw, ULONG64 rsp)
1462 unsigned int len;
1464 /* Version = 1, no flags, no prologue. */
1465 if (unw[0] != 1 || unw[1] != 0)
1466 return;
1467 len = unw[2];
1468 /* No frame pointer. */
1469 if (unw[3] != 0)
1470 return;
1471 unw += 4;
1472 while (len > 0)
1474 /* Offset in prologue = 0. */
1475 if (unw[0] != 0)
1476 return;
1477 switch (unw[1] & 0xf)
1479 case UWOP_ALLOC_LARGE:
1480 /* Expect < 512KB. */
1481 if ((unw[1] & 0xf0) != 0)
1482 return;
1483 rsp += *(unsigned short *)(unw + 2) * 8;
1484 len--;
1485 unw += 2;
1486 break;
1487 case UWOP_SAVE_NONVOL:
1488 case UWOP_SAVE_XMM128:
1489 len--;
1490 unw += 2;
1491 break;
1492 case UWOP_PUSH_MACHFRAME:
1494 ULONG64 *rip;
1495 rip = (ULONG64 *)rsp;
1496 if ((unw[1] & 0xf0) == 0x10)
1497 rip++;
1498 /* Adjust rip. */
1499 (*rip)++;
1501 return;
1502 default:
1503 /* Unexpected. */
1504 return;
1506 unw += 2;
1507 len--;
1511 EXCEPTION_DISPOSITION
1512 __gnat_personality_seh0 (PEXCEPTION_RECORD ms_exc, void *this_frame,
1513 PCONTEXT ms_orig_context,
1514 PDISPATCHER_CONTEXT ms_disp)
1516 /* Possibly transform run-time errors into Ada exceptions. As a small
1517 optimization, we call __gnat_SEH_error_handler only on non-user
1518 exceptions. */
1519 if (!(ms_exc->ExceptionCode & STATUS_USER_DEFINED))
1521 struct Exception_Data *exception;
1522 const char *msg;
1523 ULONG64 excpip = (ULONG64) ms_exc->ExceptionAddress;
1525 if (excpip != 0
1526 && excpip >= (ms_disp->ImageBase
1527 + ms_disp->FunctionEntry->BeginAddress)
1528 && excpip < (ms_disp->ImageBase
1529 + ms_disp->FunctionEntry->EndAddress))
1531 /* This is a fault in this function. We need to adjust the return
1532 address before raising the GCC exception. */
1533 CONTEXT context;
1534 PRUNTIME_FUNCTION mf_func = NULL;
1535 ULONG64 mf_imagebase;
1536 ULONG64 mf_rsp = 0;
1538 /* Get the context. */
1539 RtlCaptureContext (&context);
1541 while (1)
1543 PRUNTIME_FUNCTION RuntimeFunction;
1544 ULONG64 ImageBase;
1545 VOID *HandlerData;
1546 ULONG64 EstablisherFrame;
1548 /* Get function metadata. */
1549 RuntimeFunction = RtlLookupFunctionEntry
1550 (context.Rip, &ImageBase, ms_disp->HistoryTable);
1551 if (RuntimeFunction == ms_disp->FunctionEntry)
1552 break;
1553 mf_func = RuntimeFunction;
1554 mf_imagebase = ImageBase;
1555 mf_rsp = context.Rsp;
1557 if (!RuntimeFunction)
1559 /* In case of failure, assume this is a leaf function. */
1560 context.Rip = *(ULONG64 *) context.Rsp;
1561 context.Rsp += 8;
1563 else
1565 /* Unwind. */
1566 RtlVirtualUnwind (0, ImageBase, context.Rip, RuntimeFunction,
1567 &context, &HandlerData, &EstablisherFrame,
1568 NULL);
1571 /* 0 means bottom of the stack. */
1572 if (context.Rip == 0)
1574 mf_func = NULL;
1575 break;
1578 if (mf_func != NULL)
1579 __gnat_adjust_context
1580 ((unsigned char *)(mf_imagebase + mf_func->UnwindData), mf_rsp);
1583 exception = __gnat_map_SEH (ms_exc, &msg);
1584 if (exception != NULL)
1586 struct _Unwind_Exception *exc;
1588 /* Directly convert the system exception to a GCC one.
1589 This is really breaking the API, but is necessary for stack size
1590 reasons: the normal way is to call Raise_From_Signal_Handler,
1591 which build the exception and calls _Unwind_RaiseException, which
1592 unwinds the stack and will call this personality routine. But
1593 the Windows unwinder needs about 2KB of stack. */
1594 exc = __gnat_create_machine_occurrence_from_signal_handler
1595 (exception, msg);
1596 memset (exc->private_, 0, sizeof (exc->private_));
1597 ms_exc->ExceptionCode = STATUS_GCC_THROW;
1598 ms_exc->NumberParameters = 1;
1599 ms_exc->ExceptionInformation[0] = (ULONG_PTR)exc;
1604 return _GCC_specific_handler (ms_exc, this_frame, ms_orig_context,
1605 ms_disp, __gnat_personality_imp);
1607 #endif /* SEH */
1609 #if !defined (__USING_SJLJ_EXCEPTIONS__)
1610 /* Size of the _Unwind_Exception structure. This is used by g-cppexc to get
1611 the offset to the C++ object. */
1613 const int __gnat_unwind_exception_size = sizeof (_Unwind_Exception);
1614 #endif