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