-
Notifications
You must be signed in to change notification settings - Fork 94
/
abi-eh.html
2378 lines (2017 loc) · 70.8 KB
/
abi-eh.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<HTML>
<HEAD>
<title>C++ ABI for Itanium: Exception Handling</title>
<link rel=stylesheet href=small-table.css type="text/css">
<link rel=stylesheet href=code.css type="text/css">
</HEAD>
<BODY>
<hr />
<h1>Itanium C++ ABI: Exception Handling ($Revision: 1.22 $)</h1>
<p> <hr> <p>
<h2> Contents </h2>
<ul>
<li> <a href=#intro> Introduction </a>
<ul>
<li> <a href=#defs> Definitions </a>
<li> <a href=#docs> Base Documents </a>
<!--
<li> <a href=#issues> Open Issues </a>
-->
</ul>
<li> <a href=#base-abi> Level I: Base ABI </a>
<ul>
<li> <a href=#base-framework> 1.1 Exception Handler Framework </a>
<li> <a href=#base-data> 1.2 Data Structures </a>
<li> <a href=#base-throw> 1.3 Throwing an Exception </a>
<li> <a href=#base-om> 1.4 Exception Object Management </a>
<li> <a href=#base-context> 1.5 Context Management </a>
<li> <a href=#base-personality> 1.6 Personality Routine </a>
</ul>
<li> <a href=#cxx-abi> Level II: C++ ABI </a>
<ul>
<li> <a href=#cxx-intro> 2.1 Introduction </a>
<li> <a href=#cxx-data> 2.2 Data Structures </a>
<li> <a href=#cxx-init> 2.3 Standard Runtime Initialization </a>
<li> <a href=#cxx-throw> 2.4 Throwing an Exception</a>
<li> <a href=#cxx-catch> 2.5 Catching an Exception</a>
<li> <a href=#cxx-aux> 2.6 Auxiliary Runtime APIs</a>
</ul>
<li> <a href=#cxx-rt> Level III: Implementation </a>
<ul>
<li> <a href=#imp-intro> 3.1 Introduction </a>
<li> <a href=#imp-data> 3.2 Data Structures </a>
<li> <a href=#imp-init> 3.3 Runtime Initialization </a>
<li> <a href=#imp-throw> 3.4 Throwing an Exception</a>
<li> <a href=#imp-catch> 3.5 Catching an Exception</a>
</ul>
<li> <a href=#revisions> Appendix R: Revision History</a>
</ul>
<p> <hr> <p>
<a name=intro></a>
<h3> Introduction </h3>
In this document, we define the C++ exception handling ABI,
at three levels:
<ol type=I>
<li> the <a href=#base-abi>base ABI</a>,
interfaces common to all languages and implementations;
<li> the <a href=#cxx-abi>C++ ABI</a>,
interfaces necessary for interoperability of C++ implementations; and
<li> the specification of a particular runtime implementation.
</ol>
<p>
This specification is based on the general model described roughly in the
<a href="https://www.intel.com/content/dam/www/public/us/en/documents/guides/itanium-software-runtime-architecture-guide.pdf">
Itanium Software Conventions and Runtime Architecture Guide</a>.
However, the Level I (base ABI) specification here contradicts that
document in some particulars,
and is being proposed as a modification.
That document describes a framework which can be used by an arbitrary
implementation, with a complete definition of the stack unwind mechanism,
but no significant constraints on the language-specific processing.
In particular, it is not sufficient to guarantee that two object files
compiled by different C++ compilers could interoperate,
e.g. throwing an exception in one of them and catching it in the other.
<p>
In Section I below, we will elaborate missing details from this base
document, largely in the form of specifying the APIs to be used in
accessing the language-independent stack unwind facilities,
namely the unwind descriptor tables and the personality routines.
This specification should be implemented by any Itanium psABI-compliant
system.
<p>
In Section II below, we will specify the API of the C++ exception
handling facilities, specifically for raising and catching exceptions.
These APIs should be implemented by any C++ system compliant with the
Itanium C++ ABI.
<i>Note that the level II and level III specifications are not
completed at this time.</i>
<p> <hr> <p>
<a name=defs></a>
<h4> Definitions </h4>
<p>
The descriptions below make use of the following definitions:
<dl>
<p>
<dt><b><i>landing pad</i></b></dt>:
<dd>
A section of user code intended to catch, or otherwise clean up after,
an exception.
It gains control from the exception runtime via the personality routine,
and after doing the appropriate processing either merges into the
normal user code or returns to the runtime by resuming
or raising a new exception.
</dl>
<p> <hr> <p>
<a name=docs></a>
<h4> Base Documents </h4>
<p>
This document is based on the
<a href=abi.html><b>C++ ABI for Itanium</b></a>,
and the Level II specification below is considered to be part of that
document (Chapter 4).
See <a href=abi.html#docs>Base Documents</a> in that document for
further references.
<!--
<font color=red>
<p> <hr> <p>
<a name=issues></a>
<h4> Open Issues </h4>
The following open issues remain to be resolved in this document:
<ol>
<p>
<li>
...
</ol>
</font>
-->
<p> <hr> <p>
<a name=base-abi></a>
<h3> Level I. Base ABI </h3>
<p>
This section defines the Unwind Library interface,
expected to be provided by any Itanium psABI-compliant system.
This is the interface on which the C++ ABI exception-handling
facilities are built.
We assume as a basis the unwind descriptor tables described in the base
<a
href="https://www.intel.com/content/dam/www/public/us/en/documents/guides/itanium-software-runtime-architecture-guide.pdf">Itanium
Software Conventions & Runtime Architecture Guide</a>.
Our focus here will on the APIs for accessing those structures.
<p>
It is intended that nothing in this section be specific to C++,
though some parts are clearly intended to support C++ features.
<p>
The unwinding library interface consists of at least the following
routines:
<code><pre>
_Unwind_RaiseException,
_Unwind_Resume,
_Unwind_DeleteException,
_Unwind_GetGR,
_Unwind_SetGR,
_Unwind_GetIP,
_Unwind_SetIP,
_Unwind_GetRegionStart,
_Unwind_GetLanguageSpecificData,
_Unwind_ForcedUnwind
</pre></code>
In addition, two datatypes are defined
(<code>_Unwind_Context</code> and <code>_Unwind_Exception</code>)
to interface a calling runtime (such as the C++ runtime)
and the above routines.
All routines and interfaces behave as if defined
<code>extern "C"</code>.
In particular, the names are not mangled.
All names defined as part of this interface have a
"<code>_Unwind_</code>" prefix.
<p>
Lastly, a language and vendor specific personality routine will be stored
by the compiler in the unwind descriptor for the stack frames requiring
exception processing.
The personality routine is called by the unwinder to handle
language-specific tasks such as identifying the frame handling a
particular exception.
<p>
<a name=base-framework></a>
<h4> 1.1 Exception Handler Framework </h4>
<p>
<h5> Reasons for Unwinding </h5>
<p>
There are two major reasons for unwinding the stack:
<ul>
<li> exceptions, as defined by languages that support them (such as C++)
<li> "forced" unwinding (such as caused by <code>longjmp</code>
or thread termination).
</ul>
The interface described here tries to keep both similar.
There is a major difference, however.
<ul>
<p>
<li>
In the case an exception is thrown,
the stack is unwound while the exception propagates,
but it is expected that the personality routine for each stack frame
knows whether it wants to catch the exception or pass it through.
This choice is thus delegated to the personality routine,
which is expected to act properly for any type of exception,
whether "native" or "foreign".
Some guidelines for "acting properly" are given below.
<p>
<li>
During "forced unwinding", on the other hand,
an external agent is driving the unwinding.
For instance, this can be the <code>longjmp</code> routine.
This external agent, not each personality routine,
knows when to stop unwinding.
The fact that a personality routine is not given a choice about
whether unwinding will proceed is indicated by the _UA_FORCE_UNWIND flag.
</ul>
<p>
To accomodate these differences, two different routines are proposed.
<code>_Unwind_RaiseException</code> performs exception-style unwinding,
under control of the personality routines.
<code>_Unwind_ForcedUnwind</code>, on the other hand,
performs unwinding,
but gives an external agent the opportunity to intercept
calls to the personality routine.
This is done using a proxy personality routine,
that intercepts calls to the personality routine,
letting the external agent override
the defaults of the stack frame's personality routine.
<p>
As a consequence, it is not necessary for each personality routine
to know about any of the possible external agents that may cause an unwind.
For instance,
the C++ personality routine need deal only with C++ exceptions
(and possibly disguising foreign exceptions),
but it does not need to know anything specific about unwinding done
on behalf of <code>longjmp</code> or pthreads cancellation.
<p>
<h5> The Unwind Process </h5>
<p>
The standard ABI exception handling / unwind process begins with
the raising of an exception,
in one of the forms mentioned above.
This call specifies an exception object and an exception class.
<p>
The runtime framework then starts a two-phase process:
<ul>
<p>
<li>
In the <i>search</i> phase,
the framework repeatedly calls the personality routine,
with the <code>_UA_SEARCH_PHASE</code> flag as described below,
first for the current PC and register state,
and then unwinding a frame to a new PC at each step,
until the personality routine reports either success
(a handler found in the queried frame)
or failure (no handler) in all frames.
It does not actually restore the unwound state,
and the personality routine must access the state through the API.
<p>
<li>
If the search phase reports failure,
e.g. because no handler was found,
it will call <code>terminate()</code> rather than commence phase 2.
<p>
If the search phase reports success,
the framework restarts in the <i>cleanup</i> phase.
Again, it repeatedly calls the personality routine,
with the <code>_UA_CLEANUP_PHASE</code> flag as described below,
first for the current PC and register state,
and then unwinding a frame to a new PC at each step,
until it gets to the frame with an identified handler.
At that point, it restores the register state,
and control is transferred to the user landing pad code.
</ul>
<p>
Each of these two phases uses both the unwind library and the
personality routines,
since the validity of a given handler and the mechanism for
transferring control to it are language-dependent,
but the method of locating and restoring previous stack frames
is language independent.
<p>
A two-phase exception-handling model is not strictly necessary to
implement C++ language semantics,
but it does provide some benefits.
For example, the first phase allows an exception-handling mechanism
to <i>dismiss</i> an exception before stack unwinding begins,
which allows <i>resumptive</i> exception handling
(correcting the exceptional condition and resuming execution at
the point where it was raised).
While C++ does not support resumptive exception handling,
other languages do, and the two-phase model allows C++ to coexist with
those languages on the stack.
<p>
Note that even with a two-phase model,
we may execute each of the two phases more than once for a single exception,
as if the exception was being thrown more than once.
For instance, since it is not possible to determine if
a given catch clause will rethrow or not without executing it,
the exception propagation effectively stops at each catch clause,
and if it needs to restart, restarts at phase 1.
This process is not needed for destructors (cleanup code),
so the phase 1 can safely process all destructor-only frames at once
and stop at the next enclosing catch clause.
<p>
For example, if the first two frames unwound contain only cleanup code,
and the third frame contains a C++ catch clause,
the personality routine in phase 1 does not indicate that it found a
handler for the first two frames.
It must do so for the third frame,
because it is unknown how the exception will propagate out of
this third frame,
e.g. by rethrowing the exception or throwing a new one in C++.
<p>
The API specified by the Itanium psABI for implementing this framework is
described in the following sections.
<p>
<a name=base-data></a>
<h4> 1.2 Data Structures </h4>
<p>
<b> Reason Codes </b>
<p>
The unwind interface uses reason codes in several contexts to identify
the reasons for failures or other actions, defined as follows:
<code><pre>
typedef enum {
_URC_NO_REASON = 0,
_URC_FOREIGN_EXCEPTION_CAUGHT = 1,
_URC_FATAL_PHASE2_ERROR = 2,
_URC_FATAL_PHASE1_ERROR = 3,
_URC_NORMAL_STOP = 4,
_URC_END_OF_STACK = 5,
_URC_HANDLER_FOUND = 6,
_URC_INSTALL_CONTEXT = 7,
_URC_CONTINUE_UNWIND = 8
} _Unwind_Reason_Code;
</pre></code>
The interpretations of these codes are described below.
<p>
<b> Exception Header </b>
<p>
The unwind interface uses a pointer to an exception header object
as its representation of an exception being thrown.
In general, the full representation of an exception object is
language- and implementation-specific,
but it will be prefixed by a header understood by the unwind interface,
defined as follows:
<code><pre>
typedef void (*_Unwind_Exception_Cleanup_Fn)
(_Unwind_Reason_Code reason,
struct _Unwind_Exception *exc);
struct _Unwind_Exception {
uint64 exception_class;
_Unwind_Exception_Cleanup_Fn exception_cleanup;
uint64 private_1;
uint64 private_2;
};
</pre></code>
<p>
An <code>_Unwind_Exception</code> object must be double-word aligned.
The first two fields are set by user code prior to raising the exception,
and the latter two should never be touched except by the runtime.
<p>
The <code>exception_class</code> field is a language- and
implementation-specific identifier of the kind of exception.
It allows a personality routine to distinguish between native and
foreign exceptions, for example.
By convention, the high 4 bytes indicate the vendor
(for instance HP\0\0),
and the low 4 bytes indicate the language.
For the C++ ABI described in this document,
the low four bytes are C++\0.
<p>
The <code>exception_cleanup</code> routine is called whenever an
exception object needs to be destroyed by a different runtime than
the runtime which created the exception object,
for instance if a Java exception is caught by a C++ <i>catch</i>
handler.
In such a case, a reason code (see above) indicates why the
exception object needs to be deleted:
<ul>
<p>
<li> <code>_URC_FOREIGN_EXCEPTION_CAUGHT</code> = 1:
This indicates that a different runtime caught this exception.
Nested foreign exceptions,
or rethrowing a foreign exception,
result in undefined behaviour.
<p>
<li> <code>_URC_FATAL_PHASE1_ERROR</code> = 3:
The personality routine encountered an error during phase 1,
other than the specific error codes defined.
<p>
<li> <code>_URC_FATAL_PHASE2_ERROR</code> = 2:
The personality routine encountered an error during phase 2,
for instance a stack corruption.
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
Normally, all errors should be reported during phase 1 by returning
from <code>_Unwind_RaiseException</code>.
However, landing pad code could cause stack corruption
between phase 1 and phase 2.
For a C++ exception, the runtime should call <code>terminate()</code>
in that case.
</ul>
<p>
The private unwinder state
(<code>private_1</code> and <code>private_2</code>)
in an exception object should be neither read by nor written to
by personality routines or other parts of the language-specific runtime.
It is used by the specific implementation of the unwinder on the host
to store internal information,
for instance to remember the final handler frame between unwinding
phases.
<p>
In addition to the above information,
a typical runtime such as the C++ runtime will add language-specific
information used to process the exception.
This is expected to be a contiguous area of memory after the
<code>_Unwind_Exception</code> object,
but this is not required as long as the matching personality routines
know how to deal with it,
and the <code>exception_cleanup</code> routine de-allocates it properly.
<p>
<b> Unwind Context </b>
<p>
The <code>_Unwind_Context</code> type is an opaque type
used to refer to a system-specific data structure
used by the system unwinder.
This context is created and destroyed by the system,
and passed to the personality routine during unwinding.
<p>
<code><pre>
struct _Unwind_Context
</pre></code>
<p>
<a name=base-throw></a>
<h4> 1.3 Throwing an Exception </h4>
<p>
<h5> _Unwind_RaiseException </h5>
<code><pre>
_Unwind_Reason_Code _Unwind_RaiseException
( struct _Unwind_Exception *exception_object );
</pre></code>
<p>
Raise an exception,
passing along the given exception object,
which should have its <code>exception_class</code> and
<code>exception_cleanup</code> fields set.
The exception object has been allocated by the language-specific runtime,
and has a language-specific format,
except that it must contain an <code>_Unwind_Exception</code> struct
(see <i>Exception Header</i> above).
<code>_Unwind_RaiseException</code> does not return,
unless an error condition is found
(such as no handler for the exception,
bad stack format, etc.).
In such a case, an <code>_Unwind_Reason_Code</code> value is returned.
Possibilities are:
<ul>
<p>
<li> <code>_URC_END_OF_STACK</code>:
The unwinder encountered the end of the stack during phase 1,
without finding a handler.
The unwind runtime will not have modified the stack.
The C++ runtime will normally call <code>uncaught_exception()</code>
in this case.
<p>
<li> <code>_URC_FATAL_PHASE1_ERROR</code>:
The unwinder encountered an unexpected error during phase 1,
e.g. stack corruption.
The unwind runtime will not have modified the stack.
The C++ runtime will normally call <code>terminate()</code> in this case.
</ul>
<p>
If the unwinder encounters an unexpected error during phase 2,
it should return <code>_URC_FATAL_PHASE2_ERROR</code> to its caller.
<i>
In C++, this will usually be <code>__cxa_throw</code>,
which will call <code>terminate()</code>.
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
The unwind runtime will likely have modified the stack
(e.g. popped frames from it) or register context,
or landing pad code may have corrupted them.
As a result, the the caller of <code>_Unwind_RaiseException</code>
can make no assumptions about the state of its stack or registers.
</i>
<p>
<a name=forced></a>
<h5> _Unwind_ForcedUnwind </h5>
<code><pre>
typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
(int version,
_Unwind_Action actions,
uint64 exceptionClass,
struct _Unwind_Exception *exceptionObject,
struct _Unwind_Context *context,
void *stop_parameter );
_Unwind_Reason_Code _Unwind_ForcedUnwind
( struct _Unwind_Exception *exception_object,
_Unwind_Stop_Fn stop,
void *stop_parameter );
</pre></code>
<p>
Raise an exception for forced unwinding,
passing along the given exception object,
which should have its <code>exception_class</code> and
<code>exception_cleanup</code> fields set.
The exception object has been allocated by the language-specific runtime,
and has a language-specific format,
except that it must contain an <code>_Unwind_Exception</code> struct
(see <i>Exception Header</i> above).
<p>
Forced unwinding is a single-phase process
(phase 2 of the normal exception-handling process).
The <code>stop</code> and <code>stop_parameter</code> parameters
control the termination of the unwind process,
instead of the usual personality routine query.
The <code>stop</code> function parameter is called for each unwind frame,
with the parameters described for the usual personality routine below,
plus an additional <code>stop_parameter</code>.
<p>
When the <code>stop</code> function identifies the destination frame,
it transfers control (according to its own, unspecified, conventions)
to the user code as appropriate without returning,
normally after calling <code>_Unwind_DeleteException</code>.
If not, it should return
an <code>_Unwind_Reason_Code</code> value as follows:
<ul>
<p>
<li> <code>_URC_NO_REASON</code>:
This is not the destination frame.
The unwind runtime will call
the frame's personality routine with the
<code>_UA_FORCE_UNWIND</code> and <code>_UA_CLEANUP_PHASE</code> flags
set in <code>actions</code>,
and then unwind to the next frame
and call the <code>stop</code> function again.
<p>
<li> <code>_URC_END_OF_STACK</code>:
In order to allow <code>_Unwind_ForcedUnwind </code> to perform
special processing when it reaches the end of the stack,
the unwind runtime will call it after the last frame is rejected,
with a NULL stack pointer in the context,
and the <code>stop</code> function must catch this condition
(i.e. by noticing the NULL stack pointer).
It may return this reason code if it cannot handle end-of-stack.
<p>
<li> <code>_URC_FATAL_PHASE2_ERROR</code>:
The <code>stop</code> function may return this code for other fatal
conditions, e.g. stack corruption.
</ul>
If the <code>stop</code> function returns any reason code other than
<code>_URC_NO_REASON</code>,
the stack state is indeterminate from the point of view of the caller
of <code>_Unwind_ForcedUnwind</code>.
Rather than attempt to return, therefore,
the unwind library
should return <code>_URC_FATAL_PHASE2_ERROR</code> to its caller.
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
<i> Example: <code>longjmp_unwind()</code>
<p>
The expected implementation of <code>longjmp_unwind()</code> is as follows.
The <code>setjmp()</code> routine will have saved the state to be
restored in its customary place, including the frame pointer.
The <code>longjmp_unwind()</code> routine will call
<code>_Unwind_ForcedUnwind </code> with a <code>stop</code> function
that compares the frame pointer in the context record with the saved
frame pointer.
If equal, it will restore the <code>setjmp()</code> state as customary,
and otherwise it will return <code>_URC_NO_REASON</code> or
<code>_URC_END_OF_STACK</code>.
</i>
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
<i>If a future requirement for two-phase forced unwinding were identified,
an alternate routine could be defined to request it,
and an <code>actions</code> parameter flag defined to support it.
</i>
<p>
<h5> _Unwind_Resume </h5>
<code><pre>
void _Unwind_Resume (struct _Unwind_Exception *exception_object);
</pre></code>
<p>
Resume propagation of an existing exception
e.g. after executing cleanup code in a partially unwound stack.
A call to this routine is inserted at the end of a landing pad that
performed cleanup, but did not resume normal execution.
It causes unwinding to proceed further.
<p>
<img src=warning.gif alt="<b>NOTE 1</b>:">
<code>_Unwind_Resume</code> should not be used to implement rethrowing.
To the unwinding runtime,
the catch code that rethrows was a handler,
and the previous unwinding session was terminated before entering it.
Rethrowing is implemented by calling <code>_Unwind_RaiseException</code>
again with the same exception object.
<p>
<img src=warning.gif alt="<b>NOTE 2</b>:">
This is the only routine in the unwind library which is expected
to be called directly by generated code:
it will be called at the end of a landing pad in a "landing-pad" model.
<p>
<a name=base-om></a>
<h4> 1.4 Exception Object Management </h4>
<p>
<h5> _Unwind_DeleteException </h5>
<code><pre>
void _Unwind_DeleteException
(struct _Unwind_Exception *exception_object);
</pre></code>
<p>
Deletes the given exception object.
If a given runtime resumes normal execution
after catching a foreign exception,
it will not know how to delete that exception.
Such an exception will be deleted by calling
<code>_Unwind_DeleteException</code>.
This is a convenience function that calls the function pointed
to by the <code>exception_cleanup</code> field of the exception header.
<p>
<a name=base-context></a>
<h4> 1.5 Context Management </h4>
These functions are used for communicating information about the unwind
context (i.e. the unwind descriptors and the user register state)
between the unwind library and the personality routine and landing pad.
They include routines to read or set the context record images of
registers in the stack frame corresponding to a given unwind context,
and to identify the location of the current unwind descriptors
and unwind frame.
<p>
<h5> _Unwind_GetGR </h5>
<code><pre>
uint64 _Unwind_GetGR
(struct _Unwind_Context *context, int index);
</pre></code>
<p>
This function returns the 64-bit value of the given general register.
The register is identified by its index:
0 to 31 are for the fixed registers, and
32 to 127 are for the stacked registers.
<p>
During the two phases of unwinding,
only GR1 has a guaranteed value,
which is the Global Pointer (GP)
of the frame referenced by the unwind context.
If the register has its NAT bit set,
the behaviour is unspecified.
<p>
<h5> _Unwind_SetGR </h5>
<code><pre>
void _Unwind_SetGR
(struct _Unwind_Context *context,
int index,
uint64 new_value);
</pre></code>
<p>
This function sets the 64-bit value of the given register,
identified by its index as for <code>_Unwind_GetGR</code>.
The NAT bit of the given register is reset.
<p>
The behaviour is guaranteed only if the function is called
during phase 2 of unwinding,
and applied to an unwind context representing a handler frame,
for which the personality routine will return <code>_URC_INSTALL_CONTEXT</code>.
In that case, only registers GR15, GR16, GR17, GR18 should be used.
These scratch registers are reserved for passing arguments between
the personality routine and the landing pads.
<p>
<h5> _Unwind_GetIP </h5>
<code><pre>
uint64 _Unwind_GetIP
(struct _Unwind_Context *context);
</pre></code>
<p>
This function returns the 64-bit value of the instruction pointer (IP).
<p>
During unwinding, the value is guaranteed to be the address of the
bundle immediately following the call site
in the function identified by the unwind context.
This value may be outside of the procedure fragment for a
function call that is known to not return
(such as <code>_Unwind_Resume</code>).
<p>
<h5> _Unwind_SetIP </h5>
<code><pre>
void _Unwind_SetIP
(struct _Unwind_Context *context,
uint64 new_value);
</pre></code>
<p>
This function sets the value of the instruction pointer (IP)
for the routine identified by the unwind context.
<p>
The behaviour is guaranteed only when this function is called for an
unwind context representing a handler frame,
for which the personality routine will return <code>_URC_INSTALL_CONTEXT</code>.
In this case, control will be transferred to the given address,
which should be the address of a landing pad.
<p>
<a name=lsda></a>
<h5> _Unwind_GetLanguageSpecificData </h5>
<code><pre>
uint64 _Unwind_GetLanguageSpecificData
(struct _Unwind_Context *context);
</pre></code>
<p>
This routine returns the address of the language-specific data area for
the current stack frame.
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
This routine is not stricly required:
it could be accessed through <code>_Unwind_GetIP</code> using
the documented format of the <code>UnwindInfoBlock</code>,
but since this work has been done for finding
the personality routine in the first place,
it makes sense to cache the result in the context.
We could also pass it as an argument to the personality routine.
<p>
<h5> _Unwind_GetRegionStart </h5>
<code><pre>
uint64 _Unwind_GetRegionStart
(struct _Unwind_Context *context);
</pre></code>
<p>
This routine returns the address of the beginning of the procedure or
code fragment described by the current unwind descriptor block.
<p>
This information is required to access any data stored relative to the
beginning of the procedure fragment.
For instance, a call site table might be stored relative to the
beginning of the procedure fragment that contains the calls.
During unwinding, the function returns the start of the procedure
fragment containing the call site in the current stack frame.
<p>
<a name=base-personality></a>
<h4> 1.6 Personality Routine </h4>
<p>
<code><pre>
_Unwind_Reason_Code (*__personality_routine)
(int version,
_Unwind_Action actions,
uint64 exceptionClass,
struct _Unwind_Exception *exceptionObject,
struct _Unwind_Context *context);
</pre></code>
<p>
The personality routine is the function in the C++ (or other language)
runtime library which serves as an interface between the system unwind
library and language-specific exception handling semantics.
It is specific to the code fragment described by an unwind info block,
and it is always referenced via the pointer in the unwind info block,
and hence it has no psABI-specified name.
<p>
<h5> 1.6.1 Parameters </h5>
<p>
The personality routine parameters are as follows:
<dl>
<p>
<dt><code>version</code>
<dd>Version number of the unwinding runtime,
used to detect a mis-match between the unwinder conventions and the
personality routine, or to provide backward compatibility.
For the conventions described in this document,
<code>version</code> will be 1.
<p>
<dt><code>actions</code>
<dd>Indicates what processing the personality routine is expected to perform,
as a bit mask.
The possible actions are described below.
<p>
<dt><code>exceptionClass</code>
<dd>An 8-byte identifier specifying the type of the thrown exception.
By convention, the high 4 bytes indicate the vendor
(for instance HP\0\0),
and the low 4 bytes indicate the language.
For the C++ ABI described in this document,
the low four bytes are C++\0.
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
This is not a null-terminated string.
Some implementations may use no null bytes.
<p>
<dt><code>exceptionObject</code>
<dd>The pointer to a memory location recording the necessary information
for processing the exception according to the semantics of a given language
(see the <i>Exception Header</i> section above).
<p>
<dt><code>context</code>
<dd>Unwinder state information for use by the personality routine.
This is an opaque handle used by the personality routine in particular
to access the frame's registers
(see the <i>Unwind Context</i> section above).
<p>
<dt>return value
<dd>The return value from the personality routine indicates
how further unwind should happen,
as well as possible error conditions.
See the following section.
</dl>
<p>
<h5> 1.6.2 Personality Routine Actions </h5>
<p>
The <code>actions</code> argument to the personality routine is
a bitwise <code>OR</code> of one or more of the following constants:
<code><pre>
typedef int _Unwind_Action;
static const _Unwind_Action _UA_SEARCH_PHASE = 1;
static const _Unwind_Action _UA_CLEANUP_PHASE = 2;
static const _Unwind_Action _UA_HANDLER_FRAME = 4;
static const _Unwind_Action _UA_FORCE_UNWIND = 8;
</pre></code>
<dl>
<p>
<dt><code> _UA_SEARCH_PHASE </code>
<dd> Indicates that the personality routine should
check if the current frame contains a handler,
and if so return <code>_URC_HANDLER_FOUND</code>,
or otherwise return <code>_URC_CONTINUE_UNWIND</code>.
<code>_UA_SEARCH_PHASE</code> cannot be set
at the same time as <code>_UA_CLEANUP_PHASE</code>.
<p>
<dt><code> _UA_CLEANUP_PHASE </code>
<dd> Indicates that the personality routine should
perform cleanup for the current frame.
The personality routine can perform this cleanup itself,
by calling nested procedures,
and return <code>_URC_CONTINUE_UNWIND</code>.
Alternatively, it can setup the registers (including the IP)
for transferring control to a "landing pad",
and return <code>_URC_INSTALL_CONTEXT</code>.
<p>
<dt><code> _UA_HANDLER_FRAME </code>
<dd> During phase 2,
indicates to the personality routine that the current frame is
the one which was flagged as the handler frame during phase 1.
The personality routine is not allowed to change its mind
between phase 1 and phase 2,
i.e. it must handle the exception in this frame in phase 2.
<p>
<dt><code> _UA_FORCE_UNWIND </code>
<dd> During phase 2,
indicates that no language is allowed to "catch" the exception.
This flag is set while unwinding the stack
for <code>longjmp</code> or during thread cancellation.
User-defined code in a catch clause may still be executed,
but the catch clause must resume unwinding with a call to
<code>_Unwind_Resume</code> when finished.
</dl>
<p>
<a name=landing></a>
<h5> 1.6.3 Transferring Control to a Landing Pad </h5>
<p>
If the personality routine determines that it should
transfer control to a landing pad (in phase 2),
it may set up registers (including IP) with suitable values
for entering the landing pad
(e.g. with landing pad parameters),
by calling the context management routines above.
It then returns <code>_URC_INSTALL_CONTEXT</code>.
<p>
Prior to executing code in the landing pad,
the unwind library restores registers not altered by the personality routine,
using the context record,
to their state in that frame before the call that threw the exception,
as follows.
All registers specified as callee-saved by the base ABI are restored,
as well as scratch registers GR15, GR16, GR17 and GR18 (see below).
Except for those exceptions,
scratch (or caller-saved) registers are not preserved,
and their contents are undefined on transfer.