-
Notifications
You must be signed in to change notification settings - Fork 0
/
abi.html
5946 lines (5116 loc) · 204 KB
/
abi.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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<title>Itanium C++ ABI</title>
<link rel=stylesheet href=code.css type="text/css">
</HEAD>
<BODY>
<hr />
<h1>Itanium C++ ABI</h1>
<p> <hr> <p>
<h2> Contents </h2>
<ul>
<li><a href="#acknowledgements">Acknowledgements</a></li>
<li> <a href=#intro> Chapter 1: Introduction </a>
<ul>
<li> <a href=#definitions> 1.1 Definitions </a>
<li> <a href=#limits> 1.2 Limits </a>
<li> <a href=#namespace> 1.3 Namespace and Header </a>
<li> <a href=#scope> 1.4 Scope of This ABI </a>
<li> <a href=#docs> 1.5 Base Documents </a>
</ul>
<li> <a href=#layout> Chapter 2: Data Layout </a>
<ul>
<li> <a href=#general> 2.1 General </a>
<li> <a href=#pod> 2.2 POD Data Types </a>
<li> <a href=#member-pointers> 2.3 Member Pointers </a>
<li> <a href=#class-types> 2.4 Non-POD Class Types </a>
<li> <a href=#vtable> 2.5 Virtual Table Layout </a>
<li> <a href=#vtable-ctor> 2.6 Virtual Tables During Object Construction </a>
<li> <a href=#array-cookies> 2.7 Array Operator <code>new</code> Cookies </a>
<li> <a href=#guards> 2.8 Initialization Guard Variables </a>
<li> <a href=#rtti> 2.9 Run-Time Type Information (RTTI) </a>
</ul>
<li> <a href=#calls> Chapter 3: Function Calling Conventions and APIs </a>
<ul>
<li> <a href=#normal-call> 3.1 Non-virtual Function Calling Conventions </a>
<li> <a href=#vcall> 3.2 Virtual Function Calling Conventions </a>
<li> <a href=#obj-ctor> 3.3 Construction and Destruction APIs</a>
<li> <a href=#demangler> 3.4 Demangler API</a>
</ul>
<li> <a href=abi-eh.html> Chapter 4: Exception Handling </a>
<li> <a href=#linkage> Chapter 5: Linkage and Object Files </a>
<ul>
<li> <a href=#mangling> 5.1 External Names (a.k.a. Mangling)</a>
<li> <a href=#vague> 5.2 Vague Linkage </a>
<li> <a href=#unwind> 5.3 Unwind Table Location </a>
</ul>
<li> <a href=#revisions> Appendix R: Revision History</a>
</ul>
<p> <hr> <p>
<a name="acknowledgements">
<h2> Acknowledgements</h2>
<p> <hr> <p>
<p>This document was developed jointly by an informal industry
coalition consisting of (in alphabetical order) CodeSourcery, Compaq,
EDG, HP, IBM, Intel, Red Hat, and SGI. Additional contributions were
provided by a variety of individuals.</p>
<p> <hr> <p>
<a name="intro">
<h2> Chapter 1: Introduction </h2>
<p> <hr> <p>
In this document, we specify the Application Binary Interface for
C++ programs,
that is, the object code interfaces between user C++ code and the
implementation-provided system and libraries.
This includes the memory layout for C++ data objects,
including both predefined and user-defined data types,
as well as internal compiler generated objects such as virtual tables.
It also includes function calling interfaces,
exception handling interfaces,
global naming,
and various object code conventions.
<p>
In general, this document is written as a generic specification,
to be usable by C++ implementations on a variety of architectures.
However, it does contain processor-specific material for the Itanium
64-bit ABI, identified as such.
Where structured data layout is described,
we generally assume Itanium psABI member sizes.
An implementation for a 32-bit ABI would typically just change the
sizes of members as appropriate
(i.e. pointers and long ints would become 32 bits),
but sometimes an order change would be required for compactness,
and we note more substantive changes.
<p> <hr> <p>
<a name="definitions">
<h3> 1.1 Definitions </h3>
<p>
The descriptions below make use of the following definitions:
<dl>
<p>
<dt> <i>alignment</i> of a type T (or object X)</dt>
<dd>
A value A such that any object X of type T has an address satisfying
the constraint that &X modulo A == 0.
<p>
<dt> <i>base class</i> of a class T</dt>
<dd>
When this document refers to base classes of a class T,
unless otherwise specified,
it means T itself as well as all of the classes from which it is derived,
directly or indirectly, virtually or non-virtually.
We use the term <i>proper base class</i>
to exclude T itself from the list.
<p>
<dt> <i>base object destructor</i> of a class T</dt>
<dd>
A function that runs the destructors for non-static data members of T and
non-virtual direct base classes of T.
<p>
<dt> <i>complete object destructor</i> of a class T</dt>
<dd>
A function that, in addition to the actions required of a base
object destructor, runs the destructors for the virtual base classes of T.
<p>
<dt> <i>deleting destructor</i> of a class T</dt>
<dd>
A function that, in addition to the actions required of a complete
object destructor, calls the appropriate deallocation function
(i.e,. <code>operator delete</code>) for T.
<p>
<dt> <i>direct base class order</i> </dt>
<dd>
When the direct base classes of a class are viewed as an ordered set,
the order assumed is the order declared, left-to-right.
<p>
<dt> <i>diamond-shaped inheritance</i> </dt>
<dd>
A class has diamond-shaped inheritance iff it has a virtual base class
that can be reached by distinct inheritance graph paths through
more than one direct base.
<p>
<dt> <i>dynamic class</i> </dt>
<dd>
A class requiring a virtual table pointer
(because it or its bases have one or more virtual member functions or
virtual base classes).
<p>
<dt> <i>empty class</i> </dt>
<dd>
A class with no non-static data members other than zero-width bitfields,
no virtual functions, no virtual base classes,
and no non-empty non-virtual proper base classes.
<p>
<dt> <i>inheritance graph</i> </dt>
<dd>
A graph with nodes representing a class and all of its subobjects,
and arcs connecting each node with its direct bases.
<p>
<dt> <i>inheritance graph order</i> </dt>
<dd>
The ordering on a class object and all its subobjects obtained
by a depth-first traversal of its inheritance graph,
from the most-derived class object to base objects,
where:
<ul>
<p>
<li> No node is visited more than once.
(So, a virtual base subobject, and all of its base subobjects,
will be visited only once.)
<p>
<li>
The subobjects of a node are visited in the order in which they
were declared.
(So, given <code>class A : public B, public C</code>,
A is walked first,
then B and its subobjects,
and then C and its subobjects.)
</ul>
<p>
Note that the traversal may be preorder or postorder.
Unless otherwise specified,
preorder (derived classes before their bases) is intended.
<p>
<a name="instantiation-dependent"></a>
<dt> <i>instantiation-dependent</i> </dt>
<dd>
An expression is <i>instantiation-dependent</i> if it is type-dependent or value-dependent,
or it has a subexpression that is type-dependent or value-dependent. For example, if
<code>p</code> is a type-dependent identifier, the expression <code>sizeof(sizeof(p))</code>
is neither type-dependent, nor value-dependent, but it is instantiation-dependent (and could
turn out to be invalid if after substitution of template arguments <code>p</code> turns out to
have an incomplete type).
Similarly, a type expressed in source code is <i>instantiation-dependent</i> if the source
form includes an <i>instantiation-dependent</i> expression. For example, the type form
<code>double[sizeof(sizeof(p))]</code> (with <code>p</code> a type dependent identifier)
is instantiation-dependent.
</dl>
<p>
<dt> <i>morally virtual</i> </dt>
<dd>
A subobject X is a <i>morally virtual</i> base of Y if X is either a
virtual base of Y, or the direct or indirect base of a virtual base of
Y.
<p>
<dt> <i>nearly empty class</i> </dt>
<dd>
A class that contains a virtual pointer, but no other data except
(possibly) virtual bases. In particular, it:
<ul>
<li> has no non-static data members other than zero-width bitfields,
<li> has no direct base classes that are not either empty, nearly empty,
or virtual,
<li> has at most one non-virtual, nearly empty direct base class, and
<li> has no proper base class that is empty, not morally virtual, and
at an offset other than zero.
</ul>
Such classes may be primary base classes even if virtual,
sharing a virtual pointer with the derived class.
<p>
<a name="POD" />
<dt> <i>POD for the purpose of layout</i><dt>
<dd>
<p>
In general, a type is considered a POD for the purposes of layout if
it is a POD type (in the sense of ISO C++ [basic.types]). However, a
POD-struct or POD-union (in the sense of ISO C++ [class]) with a
bitfield member whose declared width is wider than the declared type
of the bitfield is not a POD for the purpose of layout. Similarly, an
array type is not a POD for the purpose of layout if the element type
of the array is not a POD for the purpose of layout. Where references
to the ISO C++ are made in this paragraph, the Technical Corrigendum 1
version of the standard is intended.
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
The ISO C++ standard published in 1998 had a different definition of
POD types. In particular, a class with a non-static data member of
pointer-to-member type was not considered a POD in C++98, but is
considered a POD in TC1. Because the C++ standard requires that
compilers not overlay the tail padding in a POD, using the C++98
definition in this ABI would prevent a conforming compiler from
correctly implementing the TC1 version of the C++ standard.
Therefore, this ABI uses the TC1 definition of POD.</dd>
</p>
<p>
<dt> <i>primary base class</i> </dt> <dd> For a dynamic class, the
unique base class (if any) with which it shares the virtual pointer at
offset 0.
<p>
<dt> <i>secondary virtual table</i> </dt>
<dd>
The instance of a virtual table for a base class
that is embedded in the virtual table of a class derived from it.
<p>
<dt> <i>thunk</i> </dt>
<dd>
A segment of code associated (in this ABI) with a target function,
which is called instead of the target function for the purpose of
modifying parameters (e.g. <code>this</code>)
or other parts of the environment
before transferring control to the target function,
and possibly making further modifications after its return.
A thunk may contain as little as an instruction to be executed prior to
falling through to an immediately following target function,
or it may be a full function with its own stack frame that does
a full call to the target function.
<p>
<dt> <i>vague linkage</i> </dt>
<dd>
The treatment of entities --
e.g. inline functions, templates, virtual tables --
with external linkage that can be
defined in multiple translation units,
while the ODR requires that the program
behave as if there were only a single definition.
<p>
<dt> <i>virtual table</i> (or <i>vtable</i>) </dt>
<dd>
A dynamic class has an associated table
(often several instances, but not one per object)
which contains information about its dynamic attributes,
e.g. virtual function pointers, virtual base class offsets, etc.
<p>
<dt> <i>virtual table group</i> </dt>
<dd>
The primary virtual table for a class along with all of the associated
secondary virtual tables for its proper base classes.
<p> <hr> <p>
<a name="limits">
<h3> 1.2 Limits </h3>
<p>
Various representations specified by this ABI impose limitations on
conforming user programs.
These include, for the 64-bit Itanium ABI:
<ul>
<p>
<li>
The offset of a non-virtual base subobject in the full object containing
it must be representable by a 56-bit signed integer
(due to RTTI implementation).
This implies a practical limit of 2**55 bytes on the size of a class.
</ul>
<p> <hr> <p>
<a name="namespace">
<h3> 1.3 Namespace and Header </h3>
<p>
This ABI specifies a number of type and function APIs supplemental
to those required by the ISO C++ Standard.
A header file named <code>cxxabi.h</code> will be provided by
implementations that declares these APIs.
The reference header file included with this ABI definition
shall be the authoritative definition of the APIs.
<p>
These APIs will be placed in a namespace <code>__cxxabiv1</code>.
The header file will also declare a namespace alias <code>abi</code>
for <code>__cxxabiv1</code>.
It is expected that users will use the alias,
and the remainder of the ABI specification will use it as well.
<p>
In general,
API objects defined as part of this ABI are assumed to be extern "C++".
However, some (many?) are specified to be extern "C" if they:
<ul>
<li> are expected to be called by users from C code,
e.g. <code>longjmp_unwind</code>; or
<li> are expected to be called only implicitly by compiled code,
and are likely to be implemented in C.
</ul>
<p> <hr> <p>
<a name="scope">
<h3> 1.4 Scope of This ABI </h3>
<a name="scope-library">
<h3> 1.4.1 Runtime Libraries </h3>
<p>
The objective of a full ABI is to allow arbitrary mixing of object
files produced by conforming implementations,
by fully specifying the <b>binary interface</b> of application programs.
We do not fully achieve this objective.
<p>
There are two principal reasons for this:
<ol type=I>
<p>
<li>
We start from the Itanium processor-specific ABI as the standard for the
underlying C interfaces.
At this time, however,
the psABI does not attempt to specify the supported C library interfaces.
<p>
<li>
More fundamental is the definition of the Standard C++ Library.
As the standard interface makes heavy use of templates,
most user object files will end up with embedded template
instantiations.
Vendors are allowed to use helper functions and data in their
implementations of these templates,
and quite reasonably do so,
with the result that a typical user object file will contain references
to such helper objects specific to the implementation where compiled.
We have not attempted to constrain the interface at this level,
because we do not consider doing so feasible at this time.
</ol>
<p>
Notwithstanding these problems,
because this ABI does completely specify the data model
and certain library interfaces that inherently interact between objects
(e.g. construction, destruction, and exceptions),
it is our intent that interoperation of object files produced by
different compilers be possible in the following cases:
<ul>
<p>
<li>
A program which uses only the standalone standard library interfaces
(Chapter 18) does not depend on the problematic template features.
<p>
<li>
Since the standard library headers for an implementation presumably
match the interfaces of the standard library on that implementation,
a program compiled with the target system's headers,
even if a mixture of compilers is used,
should function properly on that system.
</ul>
<p>
Even these cases can fail if the compiler makes use of
implementation-defined library interfaces to implement runtime
functionality without explicit user reference,
e.g. a software divide function.
We can distinguish between:
<ul>
<li> the standard support library,
which provides interfaces required by the C++ Standard Library
specification and the vendor header files required for it,
as well as interfaces required by this ABI; and
<p>
<li> the implicit compiler support library,
which provides other interfaces implicitly assumed by the compiler
and used to implement either standard features or extensions.
</ul>
<p>
An implementation shall place its standard support library in a DSO
named <code>libcxa.so</code> on Itanium systems,
or in auxiliary DSOs automatically loaded by it.
It shall place implicit compiler support
in a library separate from the standard support library,
with any external names chosen to avoid conflicts between vendors
(e.g. by including a vendor identifier as part of the names).
This allows a program to function properly if linked with the
target's standard support library and the implicit compiler support
libraries from any implementations used to build components.
<a name="scope-templates">
<h3> 1.4.2 Export Templates </h3>
<p>
This ABI does not specify the treatment of export templates,
as there are no working implementations to serve as models at this time.
We hope to address this weakness in the future when implementation
experience is available.
<p> <hr> <p>
<a name="docs">
<h3> 1.5 Base Documents </h3>
<p>
A number of other documents provide a basis on which this ABI is built,
and are occasionally referenced herein:
<ul>
<p>
<li> [gABI]
The <b>System V Application Binary Interface</b>,
otherwise known as the <i>Generic ABI</i>.
This document describes processor-independent object file formats
and binary software interfaces for C under Unix.
A somewhat out-of-date version is available from the SCO website,
<a href=http://www.caldera.com/developers/devspecs/>
http://www.caldera.com/developers/devspecs/</a>.
A newer version, produced in conjunction with the next document,
should be released in the future.
Included by reference in this ABI.
<p>
<li> [psABI]
The Intel <b>Unix System V Application Binary Interface,
Itanium Processor Supplement</b>.
This document describes Itanium processor-specific object file formats
and binary software interfaces, primarily for C, under Unix.
Available from the Intel Itanium software developer website,
<a href="http://www.intel.com/design/itanium/downloads/245370.htm">
http://www.intel.com/design/itanium/downloads/245370.htm</a>.
Included by reference in this ABI.
<p>
<li> [SWCONV]
The Intel <b>Itanium Software Conventions and Runtime Architecture Guide</b>.
This document describes Itanium processor-specific binary software interfaces,
notably including register usage, subprogram calling conventions, and
stack unwind facilities, under all systems.
Available from the Intel Itanium software developer website,
<a href="http://download.intel.com/design/Itanium/Downloads/245358.pdf">
http://download.intel.com/design/Itanium/Downloads/245358.pdf</a>.
Included by reference in this ABI.
<p>
<li> [ABI-EH]
The <a href=abi-eh.html><b>C++ ABI for Itanium: Exception Handling</b></a>.
Its Level II is considered an integral part of this document (Chapter 4).
It also contains the base specification of unwind support for [psABI].
<p>
<li> [C++FDIS]
The <b>Final Draft International Standard, Programming Language C++</b>,
ISO/IEC FDIS 14882:1998(E).
References herein to the "C++ Standard," or to just the "Standard,"
are to this document.
</ul>
<p> <hr> <p>
<a name="layout">
<h2> Chapter 2: Data Layout </h2>
<p> <hr> <p>
<a name="general">
<h3> 2.1 General </h3>
<p>
In what follows, we define the memory layout for C++ data objects.
Specifically, for each type, we specify the following information about
an object O of that type:
<ul>
<li> the <i>size</i> of an object, <i>sizeof</i>(O);
<li> the <i>alignment</i> of an object, <i>align</i>(O); and
<li> the <i>offset</i> within O, <i>offset</i>(C),
of each data component C, i.e. base or member.
</ul>
<p> For purposes internal to the specification,
we also specify:
<ul>
<li> <i>dsize</i>(O):
the <i>data size</i> of an object, which is the size of O without tail
padding.
<p>
<li> <i>nvsize</i>(O):
the <i>non-virtual size</i> of an object, which is the size of O
without virtual bases.
<p>
<li> <i>nvalign</i>(O):
the <i>non-virtual alignment</i> of an object, which is the alignment of O
without virtual bases.
</ul>
<p> <hr> <p>
<a name="pod">
<h3> 2.2 POD Data Types </h3>
<p>
The size and alignment of a type which is a <a href="#POD">POD for the
purpose of layout<a> is as specified by the base (C) ABI. Type bool
has size and alignment 1. All of these types have data size and
non-virtual size equal to their size. (We ignore tail padding for
PODs because the Standard does not allow us to use it for anything
else.)
<p> <hr> <p>
<a name=member-pointers></a>
<h3> 2.3 Member Pointers </h3>
<p>
A pointer to data member is an offset from the base
address of the class object containing it,
represented as a <code>ptrdiff_t</code>.
It has the size and alignment attributes of a <code>ptrdiff_t</code>.
A NULL pointer is represented as -1.
<p>
A pointer to member function is a pair <ptr, adj> as follows:
<dl>
<p>
<dt> <code>ptr</code>:
<dd> For a non-virtual function, this field is a simple function pointer.
(Under current base Itanium psABI conventions,
that is a pointer to a GP/function address pair.)
For a virtual function,
it is 1 plus the virtual table offset (in bytes) of the function,
represented as a <code>ptrdiff_t</code>.
The value zero represents a NULL pointer,
independent of the adjustment field value below.
<p>
<dt> <code>adj</code>:
<dd> The required adjustment to <i>this</i>,
represented as a <code>ptrdiff_t</code>.
</dl>
<p>
It has the size, data size, and alignment
of a class containing those two members, in that order.
(For 64-bit Itanium, that will be 16, 16, and 8 bytes respectively.)
<p> <hr> <p>
<a name="class-types">
<h3> 2.4 Non-POD Class Types </h3>
For a class type C which is not a <a href="#POD">POD for the purpose
of layout</a>, assume that all component types (i.e. proper base
classes and non-static data member types) have been laid out, defining
size, data size, non-virtual size, alignment, and non-virtual
alignment.
(See the description of these terms in
<a href=#general><b>General</b></a> above.)
Further, assume for data members that nvsize==size, and nvalign==align.
Layout (of type C) is done using the following procedure.
<ol type=I>
<p>
<li> <h5> Initialization </h5>
<ol type=1>
<p>
<li> Initialize sizeof(C) to zero, align(C) to one, dsize(C) to zero.
<p>
<li> If C is a dynamic class type:
<ol type=a>
<p>
<li> Identify all virtual base classes, direct or indirect,
that are primary base classes for some other direct or indirect
base class.
Call these <i>indirect primary base classes</i>.
<p>
<li> If C has a dynamic base class,
attempt to choose a primary base class B.
It is the first (in direct base class order)
non-virtual dynamic base class, if one exists.
Otherwise, it is a nearly empty virtual base class,
the first one in (preorder) inheritance graph order which
is not an indirect primary base class if any exist,
or just the first one if they are all indirect primaries.
<p>
<li> If C has no primary base class, allocate the virtual table
pointer for C at offset zero, and set sizeof(C), align(C),
and dsize(C) to the appropriate values for a pointer (all 8
bytes for Itanium 64-bit ABI).
</ol>
</ol>
<p>
<img src=warning.gif alt="<b>NOTE</b>:">
<i>
Case (2b) above is now considered to be an error in the design. The
use of the first indirect primary base class as the derived class'
primary base does not save any space in the object, and will cause
some duplication of virtual function pointers in the additional copy
of the base classes virtual table.
<p>
The benefit is that using the derived class virtual pointer as the base
class virtual pointer will often save a load,
and no adjustment to the <code>this</code> pointer will be required for
calls to its virtual functions.
<p>
It was thought that 2b would allow the compiler to avoid
adjusting <code>this</code> in some cases, but this was incorrect, as
the <a href=#vcall>virtual function call algorithm</a> requires that
the function be looked up through a pointer to a class that defines
the function, not one that just inherits it. Removing that
requirement would not be a good idea, as there would then no longer be
a way to emit all thunks with the functions they jump to. For
instance, consider this example:
<blockquote><code><pre>
struct A { virtual void f(); };
struct B : virtual public A { int i; };
struct C : virtual public A { int j; };
struct D : public B, public C {};
</pre></code></blockquote>
<p>
When B and C are declared, A is a primary base in each case, so although
vcall offsets are allocated in the A-in-B and A-in-C vtables, no
<code>this</code> adjustment is required and no thunk is generated.
However, inside D objects, A is no longer a primary base of C, so if we
allowed calls to <code>C::f()</code> to use the copy of A's vtable in the C
subobject, we would need to adjust <code>this</code> from <code>C*</code>
to <code>B::A*</code>, which would require a third-party thunk. Since we
require that a call to <code>C::f()</code> first convert to
<code>A*</code>, C-in-D's copy of A's vtable is never referenced, so this
is not necessary.
</i>
<p>
<li> <h5> Allocation of Members Other Than Virtual Bases </h5>
<p>
For each data component D (first the primary base of C, if any, then
the non-primary, non-virtual direct base classes in declaration order,
then the non-static data members and unnamed bitfields in declaration
order), allocate as follows:
<ol type=1>
<p>
<li> If D is a (possibly unnamed) bitfield whose declared type is
<code>T</code> and whose declared width is <code>n</code> bits:
<p>
There are two cases depending on <code>sizeof(T)</code>
and <code>n</code>:
<ol type=a>
<p>
<li>
If <code>sizeof(T)*8 >= n</code>,
the bitfield is allocated as required by the underlying C psABI,
subject to the constraint that a bitfield is never placed in the
tail padding of a base class of C.
<p>
If dsize(C) > 0, and the byte at offset dsize(C) - 1 is
partially filled by a bitfield, and that bitfield is also a
data member declared in C (but not in one of C's proper base
classes), the next available bits are the unfilled bits at
offset dsize(C) - 1. Otherwise, the next available bits are at
offset dsize(C).
<p>
Update align(C) to max (align(C), align(T)).
<p>
<li>
If <code>sizeof(T)*8 < n</code>,
let T' be the largest integral POD type with
<code>sizeof(T')*8 <= n</code>.
The bitfield is allocated starting at the next offset aligned
appropriately for T', with length n bits.
The first <code>sizeof(T)*8</code> bits are used to hold the
value of the bitfield,
followed by <code>n - sizeof(T)*8</code> bits of padding.
<p>
Update align(C) to max (align(C), align(T')).
</ol>
<p>
In either case,
update dsize(C) to include the last byte
containing (part of) the bitfield,
and update sizeof(C) to max(sizeof(C),dsize(C)).
<p>
<li> If D is not an empty base class or D is a data member:
<p>
Start at offset dsize(C),
incremented if necessary for alignment
to nvalign(D) for base classes or
to align(D) for data members.
Place D at this offset unless doing so would result in two
components (direct or indirect) of the same type having the
same offset.
If such a component type conflict occurs,
increment the candidate offset by nvalign(D)
for base classes or by align(D) for data members
and try again,
repeating until success occurs
(which will occur no later than sizeof(C) rounded up to the
required alignment).
<p>
If D is a base class, this step allocates only its non-virtual
part, i.e. excluding any direct or indirect virtual bases.
<p>
If D is a base class, update sizeof(C) to max (sizeof(C),
offset(D)+nvsize(D)). Otherwise, if D is a data member,
update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)).
<p>
If D is a base class (not empty in this case),
update dsize(C) to offset(D)+nvsize(D),
and align(C) to max (align(C), nvalign(D)).
If D is a data member,
update dsize(C) to offset(D)+sizeof(D),
align(C) to max (align(C), align(D)).
</p>
<p>
<li> If D is an empty proper base class:
<p>
Its allocation is similar to case (2) above,
except that additional candidate offsets are considered before
starting at dsize(C).
First, attempt to place D at offset zero.
If unsuccessful (due to a component type conflict),
proceed with attempts at dsize(C) as for non-empty bases.
As for that case, if there is a type conflict at dsize(C)
(with alignment updated as necessary),
increment the candidate offset by nvalign(D),
and try again,
repeating until success occurs.
<p>
Once offset(D) has been chosen,
update sizeof(C) to max (sizeof(C), offset(D)+sizeof(D)).
Note that nvalign(D) is 1, so no update of align(C) is needed.
Similarly, since D is an empty base class,
no update of dsize(C) is needed.
</ol>
<p>
After all such components have been allocated, set nvalign(C) =
align(C) and nvsize(C) = sizeof(C). The values of nvalign(C) and
nvsize(C) will not change during virtual base allocation. Note that
nvsize(C) need not be a multiple of nvalign(C).
<p><a name="a17">
<li> <h5> Virtual Base Allocation </h5>
<p>
<p>
Finally allocate any direct or indirect virtual base classes
(except the primary base class or any indirect primary base classes)
as we did non-virtual base classes
in step II-2 (if not empty) or II-3 (if empty),
in inheritance graph order.
Update sizeof(C) to max (sizeof(C), offset(D)+nvsize(D)).
If non-empty, also update align(C) and dsize(C) as in II-2.
<p>
The primary base class has already been allocated in I-2b. Any
indirect primary base class E of the current class C, i.e. one that
has been chosen as the primary base class of some other base class
(direct or indirect, virtual or non-virtual) of C, will be allocated
as part of that other base class, and is not allocated here. If E is
a primary base class of more than one other base, the instance used as
its allocation in C shall be the first such in the inheritance graph
order.
<i>
<p>
Consider:
<pre><code>
struct R { virtual void r (); };
struct S { virtual void s (); };
struct T : virtual public S { virtual void t (); };
struct U : public R, virtual public T { virtual void u (); };
</code></pre>
R is the primary base class for U since it is the first direct
non-virtual dynamic base.
Then, since an inheritance-order walk of U is { U, R, T, S }
the T base is allocated next.
Since S is a primary base of T,
there is no need to allocate it separately.
However, given:
<pre><code>
struct V : public R, virtual public S, virtual public T {
virtual void v ();
};
</code></pre>
the inheritance-order walk of V is { V, R, S, T }.
Nevertheless, although S is considered for allocation first as a virtual base,
it is not allocated separately because it is a primary base of T,
another base.
Thus sizeof (V) == sizeof (U),
and the full layout is equivalent to the C struct:
</i>
<pre><code>
struct X {
R r;
T t;
};
</code></pre>
<p>
<li> <h5> Finalization </h5>
<p>
Round sizeof(C) up to a non-zero multiple of align(C). If C is a POD,
but not a POD for the purpose of layout, set nvsize(C) = sizeof(C).
</ol>
<p> <hr> <p>
<a name="vtable">
<h3> 2.5 Virtual Table Layout </h3>
<p>
<h4> 2.5.1 General </h4>
<p>
A <i>virtual table</i> (<i>vtable</i>) is a table of information used
to dispatch virtual functions,
to access virtual base class subobjects,
and to access information for runtime type identification (RTTI).
Each class that has virtual member functions or virtual bases
has an associated set of virtual tables.
There may be multiple virtual tables for a particular class,
if it is used as a base class for other classes.
However, the virtual table pointers within all the objects (instances)
of a particular most-derived class point to the same set of virtual tables.
<p>
A virtual table consists of a sequence of offsets, data pointers,
and function pointers, as well as structures composed of such items.
We will describe below the sequence of such items.
Their offsets within the virtual table are determined by that allocation
sequence and the natural ABI size and alignment,
just as a data struct would be. In particular:
<ul>
<li> Offsets are of type <code>ptrdiff_t</code> unless otherwise stated.
<li> Data pointers have normal pointer size and alignment.
<li> On Itanium, function pointers are pairs: the function address follwed
by the global pointer value that should be used when calling the
function, aligned as for a pointer. On other
platforms, function pointers are represented as they would be in any
other context.
</ul>
<p>
In general, what we consider the address of a virtual table
(i.e. the address contained in objects pointing to a virtual table)
may not be the beginning of the virtual table.
We call it the <i>address point</i> of the virtual table.
The virtual table may therefore contain components at either positive or
negative offsets from its address point.
<p>
<h4> 2.5.2 Virtual Table Components and Order </h4>
<p>
This section describes the usage and relative order of various
components that may appear in virtual tables.
Precisely which components are present in various possible virtual
tables is specified in the next section.
If present, components are present in the order described,
except for the exceptions specified.
<ul>
<p>
<li>
<i>Virtual call (vcall) offsets</i> are used
to perform pointer adjustment for virtual functions
that are declared in a virtual base class or its subobjects
and overridden in a class derived from it.
These entries are allocated in the virtual table for the virtual base class
that is most immediately derived from the base class containing the
overridden virtual function declaration.
They are used
to find the necessary adjustment from the virtual base to the
derived class containing the overrider,
if any.
When a virtual function is invoked via a virtual base,
but has been overridden in a derived class,
the overriding function first adds a fixed offset to adjust the
<code>this</code> pointer to the virtual base,
and then adds the value contained at the vcall offset
in the virtual base to its <code>this</code> pointer
to get the address of the derived object where the function was overridden.
These values may be positive or negative.
These are first in the virtual table if present,
ordered as specified in categories 3 and 4 of Section 2.5.3 below.