xref: /netbsd-src/external/gpl3/binutils.old/dist/libctf/doc/ctf-spec.texi (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1*c42dbd0eSchristos\input texinfo       @c                    -*- Texinfo -*-
2*c42dbd0eSchristos@setfilename ctf-spec.info
3*c42dbd0eSchristos@settitle The CTF File Format
4*c42dbd0eSchristos@ifnottex
5*c42dbd0eSchristos@xrefautomaticsectiontitle on
6*c42dbd0eSchristos@end ifnottex
7*c42dbd0eSchristos@synindex fn cp
8*c42dbd0eSchristos@synindex tp cp
9*c42dbd0eSchristos@synindex vr cp
10*c42dbd0eSchristos
11*c42dbd0eSchristos@copying
12*c42dbd0eSchristosCopyright @copyright{} 2021-2022 Free Software Foundation, Inc.
13*c42dbd0eSchristos
14*c42dbd0eSchristosPermission is granted to copy, distribute and/or modify this document
15*c42dbd0eSchristosunder the terms of the GNU General Public License, Version 3 or any
16*c42dbd0eSchristoslater version published by the Free Software Foundation.  A copy of the
17*c42dbd0eSchristoslicense is included in the section entitled ``GNU General Public
18*c42dbd0eSchristosLicense''.
19*c42dbd0eSchristos
20*c42dbd0eSchristos@end copying
21*c42dbd0eSchristos
22*c42dbd0eSchristos@dircategory Software development
23*c42dbd0eSchristos@direntry
24*c42dbd0eSchristos* CTF: (ctf-spec).         The CTF file format.
25*c42dbd0eSchristos@end direntry
26*c42dbd0eSchristos
27*c42dbd0eSchristos@titlepage
28*c42dbd0eSchristos@title The CTF File Format
29*c42dbd0eSchristos@subtitle Version 3
30*c42dbd0eSchristos@author Nick Alcock
31*c42dbd0eSchristos
32*c42dbd0eSchristos@page
33*c42dbd0eSchristos@vskip 0pt plus 1filll
34*c42dbd0eSchristos@insertcopying
35*c42dbd0eSchristos@end titlepage
36*c42dbd0eSchristos@contents
37*c42dbd0eSchristos
38*c42dbd0eSchristos@ifnottex
39*c42dbd0eSchristos@node Top
40*c42dbd0eSchristos@top The CTF file format
41*c42dbd0eSchristos
42*c42dbd0eSchristosThis manual describes version 3 of the CTF file format, which is
43*c42dbd0eSchristosintended to model the C type system in a fashion that C programs can
44*c42dbd0eSchristosconsume at runtime.
45*c42dbd0eSchristos@end ifnottex
46*c42dbd0eSchristos
47*c42dbd0eSchristos@node Overview
48*c42dbd0eSchristos@unnumbered Overview
49*c42dbd0eSchristos@cindex Overview
50*c42dbd0eSchristos
51*c42dbd0eSchristosThe CTF file format compactly describes C types and the association
52*c42dbd0eSchristosbetween function and data symbols and types: if embedded in ELF objects,
53*c42dbd0eSchristosit can exploit the ELF string table to reduce duplication further.
54*c42dbd0eSchristosThere is no real concept of namespacing: only top-level types are
55*c42dbd0eSchristosdescribed, not types scoped to within single functions.
56*c42dbd0eSchristos
57*c42dbd0eSchristosCTF dictionaries can be @dfn{children} of other dictionaries, in a
58*c42dbd0eSchristosone-level hierarchy: child dictionaries can refer to types in the
59*c42dbd0eSchristosparent, but the opposite is not sensible (since if you refer to a child
60*c42dbd0eSchristostype in the parent, the actual type you cited would vary depending on
61*c42dbd0eSchristoswhat child was attached).  This parent/child definition is recorded in
62*c42dbd0eSchristosthe child, but only as a recommendation: users of the API have to attach
63*c42dbd0eSchristosparents to children explicitly, and can choose to attach a child to any
64*c42dbd0eSchristosparent they like, or to none, though doing so might lead to unpleasant
65*c42dbd0eSchristosconsequences like dangling references to types.  @xref{Type indexes and
66*c42dbd0eSchristostype IDs}.  Type lookups in child dicts that are not associated with a
67*c42dbd0eSchristosparent at all will fail with @code{ECTF_NOPARENT} if a parent type was
68*c42dbd0eSchristosneeded.
69*c42dbd0eSchristos
70*c42dbd0eSchristosThe associated API to generate, merge together, and query this file
71*c42dbd0eSchristosformat will be described in the accompanying @code{libctf} manual once
72*c42dbd0eSchristosit is written.  There is no API to modify dictionaries once they've been
73*c42dbd0eSchristoswritten out: CTF is a write-once file format.  (However, it is always
74*c42dbd0eSchristospossible to dynamically create a new child dictionary on the fly and
75*c42dbd0eSchristosattach it to a pre-existing, read-only parent.)
76*c42dbd0eSchristos
77*c42dbd0eSchristosThere are two major pieces to CTF: the @dfn{archive} and the
78*c42dbd0eSchristos@dfn{dictionary}.  Some relatives and ancestors of CTF call dictionaries
79*c42dbd0eSchristos@dfn{containers}: the archive format is unique to this variant of CTF.
80*c42dbd0eSchristos(Much of the source code still uses the old term.)
81*c42dbd0eSchristos
82*c42dbd0eSchristosThe archive file format is a very simple mmappable archive used to group
83*c42dbd0eSchristosmultiple dictionaries together into groups: it is expected to slowly go
84*c42dbd0eSchristosaway and be replaced by other mechanisms, but right now it is an
85*c42dbd0eSchristosimportant part of the file format, used to group dictionaries containing
86*c42dbd0eSchristostypes with conflicting definitions in different TUs with the overarching
87*c42dbd0eSchristosdictionary used to store all other types.  (Even when archives go away,
88*c42dbd0eSchristosthe @code{libctf} API used to access them will remain, and access the
89*c42dbd0eSchristosother mechanisms that replace it instead.)
90*c42dbd0eSchristos
91*c42dbd0eSchristosThe CTF dictionary consists of a @dfn{preamble}, which does not vary
92*c42dbd0eSchristosbetween versions of the CTF file format, and a @dfn{header} and some
93*c42dbd0eSchristosnumber of @dfn{sections}, which can vary between versions.
94*c42dbd0eSchristos
95*c42dbd0eSchristosThe rest of this specification describes the format of these sections,
96*c42dbd0eSchristosfirst for the latest version of CTF, then for all earlier versions
97*c42dbd0eSchristossupported by @code{libctf}: the earlier versions are defined in terms of
98*c42dbd0eSchristostheir differences from the next later one.  We describe each part of the
99*c42dbd0eSchristosformat first by reproducing the C structure which defines that part,
100*c42dbd0eSchristosthen describing it at greater length in terms of file offsets.
101*c42dbd0eSchristos
102*c42dbd0eSchristosThe description of the file format ends with a description of relevant
103*c42dbd0eSchristoslimits that apply to it.  These limits can vary between file format
104*c42dbd0eSchristosversions.
105*c42dbd0eSchristos
106*c42dbd0eSchristosThis document is quite young, so for now the C code in @file{ctf.h}
107*c42dbd0eSchristosshould be presumed correct when this document conflicts with it.
108*c42dbd0eSchristos
109*c42dbd0eSchristos@node CTF archive
110*c42dbd0eSchristos@chapter CTF archives
111*c42dbd0eSchristos@cindex archive, CTF archive
112*c42dbd0eSchristos
113*c42dbd0eSchristosThe CTF archive format maps names to CTF dictionaries.  The names may
114*c42dbd0eSchristoscontain any character other than \0, but for now archives containing
115*c42dbd0eSchristosslashes in the names may not extract correctly.  It is possible to
116*c42dbd0eSchristosinsert multiple members with the same name, but these are quite hard to
117*c42dbd0eSchristosaccess reliably (you have to iterate through all the members rather than
118*c42dbd0eSchristosopening by name) so this is not recommended.
119*c42dbd0eSchristos
120*c42dbd0eSchristosCTF archives are not themselves compressed: the constituent components,
121*c42dbd0eSchristosCTF dictionaries, can be compressed.  (@xref{CTF header}).
122*c42dbd0eSchristos
123*c42dbd0eSchristosCTF archives usually contain a collection of related dictionaries, one
124*c42dbd0eSchristosparent and many children of that parent.  CTF archives can have a member
125*c42dbd0eSchristoswith a @dfn{default name}, @code{.ctf} (which can be represented as
126*c42dbd0eSchristos@code{NULL} in the API).  If present, this member is usually the parent
127*c42dbd0eSchristosof all the children, but it is possible for CTF producers to emit
128*c42dbd0eSchristosparents with different names if they wish (usually for backward-
129*c42dbd0eSchristoscompatibility purposes).
130*c42dbd0eSchristos
131*c42dbd0eSchristos@code{.ctf} sections in ELF objects consist of a single CTF dictionary
132*c42dbd0eSchristosrather than an archive of dictionaries if and only if the section
133*c42dbd0eSchristoscontains no types with identical names but conflicting definitions: if
134*c42dbd0eSchristostwo conflicting definitions exist, the deduplicator will place the type
135*c42dbd0eSchristosmost commonly referred to by other types in the parent and will place
136*c42dbd0eSchristosthe other type in a child named after the translation unit it is found
137*c42dbd0eSchristosin, and will emit a CTF archive containing both dictionaries instead of
138*c42dbd0eSchristosa raw dictionary.  All types that refer to such conflicting types are
139*c42dbd0eSchristosalso placed in the per-translation-unit child.
140*c42dbd0eSchristos
141*c42dbd0eSchristosThe definition of an archive in @file{ctf.h} is as follows:
142*c42dbd0eSchristos
143*c42dbd0eSchristos@verbatim
144*c42dbd0eSchristosstruct ctf_archive
145*c42dbd0eSchristos{
146*c42dbd0eSchristos  uint64_t ctfa_magic;
147*c42dbd0eSchristos  uint64_t ctfa_model;
148*c42dbd0eSchristos  uint64_t ctfa_nfiles;
149*c42dbd0eSchristos  uint64_t ctfa_names;
150*c42dbd0eSchristos  uint64_t ctfa_ctfs;
151*c42dbd0eSchristos};
152*c42dbd0eSchristos
153*c42dbd0eSchristostypedef struct ctf_archive_modent
154*c42dbd0eSchristos{
155*c42dbd0eSchristos  uint64_t name_offset;
156*c42dbd0eSchristos  uint64_t ctf_offset;
157*c42dbd0eSchristos} ctf_archive_modent_t;
158*c42dbd0eSchristos@end verbatim
159*c42dbd0eSchristos
160*c42dbd0eSchristos(Note one irregularity here: the @code{ctf_archive_t} is not a typedef
161*c42dbd0eSchristosto @code{struct ctf_archive}, but a different typedef, private to
162*c42dbd0eSchristos@code{libctf}, so that things that are not really archives can be made
163*c42dbd0eSchristosto appear as if they were.)
164*c42dbd0eSchristos
165*c42dbd0eSchristosAll the above items are always in little-endian byte order, regardless
166*c42dbd0eSchristosof the machine endianness.
167*c42dbd0eSchristos
168*c42dbd0eSchristosThe archive header has the following fields:
169*c42dbd0eSchristos
170*c42dbd0eSchristos@tindex struct ctf_archive
171*c42dbd0eSchristos@multitable {Offset} {@code{uint64_t ctfa_nfiles}} {The data model for this archive: an arbitrary integer}
172*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
173*c42dbd0eSchristos@item 0x00
174*c42dbd0eSchristos@tab @code{uint64_t ctfa_magic}
175*c42dbd0eSchristos@vindex ctfa_magic
176*c42dbd0eSchristos@vindex struct ctf_archive, ctfa_magic
177*c42dbd0eSchristos@tab The magic number for archives, @code{CTFA_MAGIC}: 0x8b47f2a4d7623eeb.
178*c42dbd0eSchristos@tindex CTFA_MAGIC
179*c42dbd0eSchristos
180*c42dbd0eSchristos@item 0x08
181*c42dbd0eSchristos@tab @code{uint64_t ctfa_model}
182*c42dbd0eSchristos@vindex ctfa_model
183*c42dbd0eSchristos@vindex struct ctf_archive, ctfa_model
184*c42dbd0eSchristos@tab The data model for this archive: an arbitrary integer that serves no
185*c42dbd0eSchristospurpose but to be handed back by the libctf API. @xref{Data models}.
186*c42dbd0eSchristos
187*c42dbd0eSchristos@item 0x10
188*c42dbd0eSchristos@tab @code{uint64_t ctfa_nfiles}
189*c42dbd0eSchristos@vindex ctfa_nfiles
190*c42dbd0eSchristos@vindex struct ctf_archive, ctfa_nfiles
191*c42dbd0eSchristos@tab The number of CTF dictionaries in this archive.
192*c42dbd0eSchristos
193*c42dbd0eSchristos@item 0x18
194*c42dbd0eSchristos@tab @code{uint64_t ctfa_names}
195*c42dbd0eSchristos@vindex ctfa_names
196*c42dbd0eSchristos@vindex struct ctf_archive, ctfa_names
197*c42dbd0eSchristos@tab Offset of the name table, in bytes from the start of the archive.
198*c42dbd0eSchristosThe name table is an array of @code{struct ctf_archive_modent_t[ctfa_nfiles]}.
199*c42dbd0eSchristos
200*c42dbd0eSchristos@item 0x20
201*c42dbd0eSchristos@tab @code{uint64_t ctfa_ctfs}
202*c42dbd0eSchristos@vindex ctfa_ctfs
203*c42dbd0eSchristos@vindex struct ctf_archive, ctfa_ctfs
204*c42dbd0eSchristos@tab Offset of the CTF table.  Each element starts with a @code{uint64_t} size,
205*c42dbd0eSchristosfollowed by a CTF dictionary.
206*c42dbd0eSchristos
207*c42dbd0eSchristos@end multitable
208*c42dbd0eSchristos
209*c42dbd0eSchristosThe array pointed to by @code{ctfa_names} is an array of entries of
210*c42dbd0eSchristos@code{ctf_archive_modent}:
211*c42dbd0eSchristos
212*c42dbd0eSchristos@tindex struct ctf_archive_modent
213*c42dbd0eSchristos@tindex ctf_archive_modent_t
214*c42dbd0eSchristos@multitable {Offset} {@code{uint64_t name_offset}} {Offset of this name, in bytes from the start}
215*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
216*c42dbd0eSchristos@item 0x00
217*c42dbd0eSchristos@tab @code{uint64_t name_offset}
218*c42dbd0eSchristos@vindex name_offset
219*c42dbd0eSchristos@vindex struct ctf_archive_modent, name_offset
220*c42dbd0eSchristos@vindex ctf_archive_modent_t, name_offset
221*c42dbd0eSchristos@tab Offset of this name, in bytes from the start of the archive.
222*c42dbd0eSchristos
223*c42dbd0eSchristos@item 0x08
224*c42dbd0eSchristos@tab @code{uint64_t ctf_offset}
225*c42dbd0eSchristos@vindex ctf_offset
226*c42dbd0eSchristos@vindex struct ctf_archive_modent, ctf_offset
227*c42dbd0eSchristos@vindex ctf_archive_modent_t, ctf_offset
228*c42dbd0eSchristos@tab Offset of this CTF dictionary, in bytes from the start of the archive.
229*c42dbd0eSchristos
230*c42dbd0eSchristos@end multitable
231*c42dbd0eSchristos
232*c42dbd0eSchristosThe @code{ctfa_names} array is sorted into ASCIIbetical order by name
233*c42dbd0eSchristos(i.e. by the result of dereferencing the @code{name_offset}).
234*c42dbd0eSchristos
235*c42dbd0eSchristosThe archive file also contains a name table and a table of CTF
236*c42dbd0eSchristosdictionaries: these are pointed to by the structures above.  The name
237*c42dbd0eSchristostable is a simple strtab which is not required to be sorted; the
238*c42dbd0eSchristosdictionary array is described above in the entry for @code{ctfa_ctfs}.
239*c42dbd0eSchristos
240*c42dbd0eSchristosThe relative order of these various parts is not defined, except that
241*c42dbd0eSchristosthe header naturally always comes first.
242*c42dbd0eSchristos
243*c42dbd0eSchristos@node CTF dictionaries
244*c42dbd0eSchristos@chapter CTF dictionaries
245*c42dbd0eSchristos@cindex dictionary, CTF dictionary
246*c42dbd0eSchristos
247*c42dbd0eSchristosCTF dictionaries consist of a header, starting with a premable, and a
248*c42dbd0eSchristosnumber of sections.
249*c42dbd0eSchristos
250*c42dbd0eSchristos@node CTF Preamble
251*c42dbd0eSchristos@section CTF Preamble
252*c42dbd0eSchristos
253*c42dbd0eSchristosThe preamble is the only part of the CTF dictionary whose format cannot
254*c42dbd0eSchristosvary between versions.  It is never compressed.  It is correspondingly
255*c42dbd0eSchristossimple:
256*c42dbd0eSchristos
257*c42dbd0eSchristos@verbatim
258*c42dbd0eSchristostypedef struct ctf_preamble
259*c42dbd0eSchristos{
260*c42dbd0eSchristos  unsigned short ctp_magic;
261*c42dbd0eSchristos  unsigned char ctp_version;
262*c42dbd0eSchristos  unsigned char ctp_flags;
263*c42dbd0eSchristos} ctf_preamble_t;
264*c42dbd0eSchristos@end verbatim
265*c42dbd0eSchristos
266*c42dbd0eSchristos@code{#define}s are provided under the names @code{cth_magic},
267*c42dbd0eSchristos@code{cth_version} and @code{cth_flags} to make the fields of the
268*c42dbd0eSchristos@code{ctf_preamble_t} appear to be part of the @code{ctf_header_t}, so
269*c42dbd0eSchristosconsuming programs rarely need to consider the existence of the preamble
270*c42dbd0eSchristosas a separate structure.
271*c42dbd0eSchristos
272*c42dbd0eSchristos@tindex struct ctf_preamble
273*c42dbd0eSchristos@tindex ctf_preamble_t
274*c42dbd0eSchristos@multitable {Offset} {@code{unsigned char ctp_version}} {The magic number for CTF dictionaries}
275*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
276*c42dbd0eSchristos@item 0x00
277*c42dbd0eSchristos@tab @code{unsigned short ctp_magic}
278*c42dbd0eSchristos@vindex ctp_magic
279*c42dbd0eSchristos@vindex cth_magic
280*c42dbd0eSchristos@vindex ctf_preamble_t, ctp_magic
281*c42dbd0eSchristos@vindex struct ctf_preamble, ctp_magic
282*c42dbd0eSchristos@vindex ctf_header_t, cth_magic
283*c42dbd0eSchristos@vindex struct ctf_header, cth_magic
284*c42dbd0eSchristos@tab The magic number for CTF dictionaries, @code{CTF_MAGIC}: 0xdff2.
285*c42dbd0eSchristos@tindex CTF_MAGIC
286*c42dbd0eSchristos
287*c42dbd0eSchristos@item 0x02
288*c42dbd0eSchristos@tab @code {unsigned char ctp_version}
289*c42dbd0eSchristos@vindex ctp_version
290*c42dbd0eSchristos@vindex cth_version
291*c42dbd0eSchristos@vindex ctf_preamble_t, ctp_version
292*c42dbd0eSchristos@vindex struct ctf_preamble, ctp_version
293*c42dbd0eSchristos@vindex ctf_header_t, cth_version
294*c42dbd0eSchristos@vindex struct ctf_header, cth_version
295*c42dbd0eSchristos@tab The version number of this CTF dictionary.
296*c42dbd0eSchristos
297*c42dbd0eSchristos@item 0x03
298*c42dbd0eSchristos@tab @code{ctp_flags}
299*c42dbd0eSchristos@vindex ctp_flags
300*c42dbd0eSchristos@vindex cth_flags
301*c42dbd0eSchristos@vindex ctf_preamble_t, ctp_flags
302*c42dbd0eSchristos@vindex struct ctf_preamble, ctp_flags
303*c42dbd0eSchristos@vindex ctf_header_t, cth_flags
304*c42dbd0eSchristos@vindex struct ctf_header, cth_flags
305*c42dbd0eSchristos@tab Flags for this CTF file. @xref{CTF file-wide flags}.
306*c42dbd0eSchristos@end multitable
307*c42dbd0eSchristos
308*c42dbd0eSchristos@cindex alignment
309*c42dbd0eSchristosEvery element of a dictionary must be naturally aligned unless otherwise
310*c42dbd0eSchristosspecified.  (This restriction will be lifted in later versions.)
311*c42dbd0eSchristos
312*c42dbd0eSchristos@cindex endianness
313*c42dbd0eSchristosCTF dictionaries are stored in the native endianness of the system that
314*c42dbd0eSchristosgenerates them: the consumer (e.g., @code{libctf}) can detect whether to
315*c42dbd0eSchristosendian-flip a CTF dictionary by inspecting the @code{ctp_magic}. (If it
316*c42dbd0eSchristosappears as 0xf2df, endian-flipping is needed.)
317*c42dbd0eSchristos
318*c42dbd0eSchristosThe version of the CTF dictionary can be determined by inspecting
319*c42dbd0eSchristos@code{ctp_version}.  The following versions are currently valid, and
320*c42dbd0eSchristos@code{libctf} can read all of them:
321*c42dbd0eSchristos
322*c42dbd0eSchristos@tindex CTF_VERSION_3
323*c42dbd0eSchristos@cindex CTF versions, versions
324*c42dbd0eSchristos@multitable {@code{CTF_VERSION_1_UPGRADED_3}} {Number} {First version, rare.  Very similar to Solaris CTF.}
325*c42dbd0eSchristos@headitem Version @tab Number @tab Description
326*c42dbd0eSchristos@item @code{CTF_VERSION_1}
327*c42dbd0eSchristos@tab 1 @tab First version, rare. Very similar to Solaris CTF.
328*c42dbd0eSchristos
329*c42dbd0eSchristos@item @code{CTF_VERSION_1_UPGRADED_3}
330*c42dbd0eSchristos@tab 2 @tab First version, upgraded to v3 or higher and written out again.
331*c42dbd0eSchristosName may change. Very rare.
332*c42dbd0eSchristos
333*c42dbd0eSchristos@item @code{CTF_VERSION_2}
334*c42dbd0eSchristos@tab 3 @tab Second version, with many range limits lifted.
335*c42dbd0eSchristos
336*c42dbd0eSchristos@item @code{CTF_VERSION_3}
337*c42dbd0eSchristos@tab 4  @tab Third and current version, documented here.
338*c42dbd0eSchristos@end multitable
339*c42dbd0eSchristos
340*c42dbd0eSchristosThis section documents @code{CTF_VERSION_3}.
341*c42dbd0eSchristos
342*c42dbd0eSchristos@vindex ctp_flags
343*c42dbd0eSchristos@node CTF file-wide flags
344*c42dbd0eSchristos@subsection CTF file-wide flags
345*c42dbd0eSchristos
346*c42dbd0eSchristosThe preamble contains bitflags in its @code{ctp_flags} field that
347*c42dbd0eSchristosdescribe various file-wide properties.  Some of the flags are valid only
348*c42dbd0eSchristosfor particular file-format versions, which means the flags can be used
349*c42dbd0eSchristosto fix file-format bugs.  Consumers that see unknown flags should
350*c42dbd0eSchristosaccordingly assume that the dictionary is not comprehensible, and
351*c42dbd0eSchristosrefuse to open them.
352*c42dbd0eSchristos
353*c42dbd0eSchristosThe following flags are currently defined.  Many are bug workarounds,
354*c42dbd0eSchristosvalid only in CTFv3, and will not be valid in any future versions: the
355*c42dbd0eSchristossame values may be reused for other flags in v4+.
356*c42dbd0eSchristos
357*c42dbd0eSchristos@multitable {@code{CTF_F_NEWFUNCINFO}} {Versions} {Value} {The external strtab is in @code{.dynstr} and the}
358*c42dbd0eSchristos@headitem Flag @tab Versions @tab Value @tab Meaning
359*c42dbd0eSchristos@tindex CTF_F_COMPRESS
360*c42dbd0eSchristos@item @code{CTF_F_COMPRESS} @tab All @tab 0x1 @tab Compressed with zlib
361*c42dbd0eSchristos@tindex CTF_F_NEWFUNCINFO
362*c42dbd0eSchristos@item @code{CTF_F_NEWFUNCINFO} @tab 3 only @tab 0x2
363*c42dbd0eSchristos@tab ``New-format'' func info section.
364*c42dbd0eSchristos@tindex CTF_F_IDXSORTED
365*c42dbd0eSchristos@item @code{CTF_F_IDXSORTED} @tab 3+ @tab 0x4 @tab The index section is
366*c42dbd0eSchristosin sorted order
367*c42dbd0eSchristos@tindex CTF_F_DYNSTR
368*c42dbd0eSchristos@item @code{CTF_F_DYNSTR} @tab 3 only @tab 0x8 @tab The external strtab is
369*c42dbd0eSchristosin @code{.dynstr} and the symtab used is @code{.dynsym}.
370*c42dbd0eSchristos@xref{The string section}
371*c42dbd0eSchristos@end multitable
372*c42dbd0eSchristos
373*c42dbd0eSchristos@code{CTF_F_NEWFUNCINFO} and @code{CTF_F_IDXSORTED} relate to the
374*c42dbd0eSchristosfunction info and data object sections. @xref{The symtypetab sections}.
375*c42dbd0eSchristos
376*c42dbd0eSchristosFurther flags (and further compression methods) wil be added in future.
377*c42dbd0eSchristos
378*c42dbd0eSchristos@node CTF header
379*c42dbd0eSchristos@section CTF header
380*c42dbd0eSchristos@cindex CTF header
381*c42dbd0eSchristos@cindex Sections, header
382*c42dbd0eSchristos
383*c42dbd0eSchristosThe CTF header is the first part of a CTF dictionary, including the
384*c42dbd0eSchristospreamble.  All parts of it other than the preamble (@pxref{CTF Preamble})
385*c42dbd0eSchristoscan vary between CTF file versions and are never compressed.  It
386*c42dbd0eSchristoscontains things that apply to the dictionary as a whole, and a table of
387*c42dbd0eSchristosthe sections into which the rest of the dictionary is divided.  The
388*c42dbd0eSchristossections tile the file: each section runs from the offset given until
389*c42dbd0eSchristosthe start of the next section.  Only the last section cannot follow this
390*c42dbd0eSchristosrule, so the header has a length for it instead.
391*c42dbd0eSchristos
392*c42dbd0eSchristosAll section offsets, here and in the rest of the CTF file, are relative to the
393*c42dbd0eSchristos@emph{end} of the header.  (This is annoyingly different to how offsets in CTF
394*c42dbd0eSchristosarchives are handled.)
395*c42dbd0eSchristos
396*c42dbd0eSchristosThis is the first structure to include offsets into the string table, which are
397*c42dbd0eSchristosnot straight references because CTF dictionaries can include references into the
398*c42dbd0eSchristosELF string table to save space, as well as into the string table internal to the
399*c42dbd0eSchristosCTF dictionary.  @xref{The string section} for more on these.  Offset 0 is
400*c42dbd0eSchristosalways the null string.
401*c42dbd0eSchristos
402*c42dbd0eSchristos@verbatim
403*c42dbd0eSchristostypedef struct ctf_header
404*c42dbd0eSchristos{
405*c42dbd0eSchristos  ctf_preamble_t cth_preamble;
406*c42dbd0eSchristos  uint32_t cth_parlabel;
407*c42dbd0eSchristos  uint32_t cth_parname;
408*c42dbd0eSchristos  uint32_t cth_cuname;
409*c42dbd0eSchristos  uint32_t cth_lbloff;
410*c42dbd0eSchristos  uint32_t cth_objtoff;
411*c42dbd0eSchristos  uint32_t cth_funcoff;
412*c42dbd0eSchristos  uint32_t cth_objtidxoff;
413*c42dbd0eSchristos  uint32_t cth_funcidxoff;
414*c42dbd0eSchristos  uint32_t cth_varoff;
415*c42dbd0eSchristos  uint32_t cth_typeoff;
416*c42dbd0eSchristos  uint32_t cth_stroff;
417*c42dbd0eSchristos  uint32_t cth_strlen;
418*c42dbd0eSchristos} ctf_header_t;
419*c42dbd0eSchristos@end verbatim
420*c42dbd0eSchristos
421*c42dbd0eSchristosIn detail:
422*c42dbd0eSchristos
423*c42dbd0eSchristos@tindex struct ctf_header
424*c42dbd0eSchristos@tindex ctf_header_t
425*c42dbd0eSchristos@multitable {Offset} {@code{ctf_preamble_t cth_preamble}} {The parent label, if deduplication happened against}
426*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
427*c42dbd0eSchristos@item 0x00
428*c42dbd0eSchristos@tab @code{ctf_preamble_t cth_preamble}
429*c42dbd0eSchristos@vindex cth_preamble
430*c42dbd0eSchristos@vindex struct ctf_header, cth_preamble
431*c42dbd0eSchristos@vindex ctf_header_t, cth_preamble
432*c42dbd0eSchristos@tab The preamble (conceptually embedded in the header). @xref{CTF Preamble}
433*c42dbd0eSchristos
434*c42dbd0eSchristos@item 0x04
435*c42dbd0eSchristos@tab @code{uint32_t cth_parlabel}
436*c42dbd0eSchristos@vindex cth_parlabel
437*c42dbd0eSchristos@vindex struct ctf_header, cth_parlabel
438*c42dbd0eSchristos@vindex ctf_header_t, cth_parlabel
439*c42dbd0eSchristos@tab The parent label, if deduplication happened against a specific label: a
440*c42dbd0eSchristosstrtab offset. @xref{The label section}. Currently unused and always 0, but may
441*c42dbd0eSchristosbe used in future when semantics are attached to the label section.
442*c42dbd0eSchristos
443*c42dbd0eSchristos@item 0x08
444*c42dbd0eSchristos@tab @code{uint32_t cth_parname}
445*c42dbd0eSchristos@vindex cth_parname
446*c42dbd0eSchristos@vindex struct ctf_header, cth_parname
447*c42dbd0eSchristos@vindex ctf_header_t, cth_parname
448*c42dbd0eSchristos@tab The name of the parent dictionary deduplicated against: a strtab offset.
449*c42dbd0eSchristosInterpretation is up to the consumer (usually a CTF archive member name).  0
450*c42dbd0eSchristos(the null string) if this is not a child dictionary.
451*c42dbd0eSchristos
452*c42dbd0eSchristos@item 0x1c
453*c42dbd0eSchristos@tab @code{uint32_t cth_cuname}
454*c42dbd0eSchristos@vindex cth_cuname
455*c42dbd0eSchristos@vindex struct ctf_header, cth_cuname
456*c42dbd0eSchristos@vindex ctf_header_t, cth_cuname
457*c42dbd0eSchristos@tab The name of the compilation unit, for consumers like GDB that want to
458*c42dbd0eSchristosknow the name of CUs associated with single CUs: a strtab offset.  0 if this
459*c42dbd0eSchristosdictionary describes types from many CUs.
460*c42dbd0eSchristos
461*c42dbd0eSchristos@item 0x10
462*c42dbd0eSchristos@tab @code{uint32_t cth_lbloff}
463*c42dbd0eSchristos@vindex cth_lbloff
464*c42dbd0eSchristos@vindex struct ctf_header, cth_lbloff
465*c42dbd0eSchristos@vindex ctf_header_t, cth_lbloff
466*c42dbd0eSchristos@tab The offset of the label section, which tiles the type space into
467*c42dbd0eSchristosnamed regions.  @xref{The label section}.
468*c42dbd0eSchristos
469*c42dbd0eSchristos@item 0x14
470*c42dbd0eSchristos@tab @code{uint32_t cth_objtoff}
471*c42dbd0eSchristos@vindex cth_objtoff
472*c42dbd0eSchristos@vindex struct ctf_header, cth_objtoff
473*c42dbd0eSchristos@vindex ctf_header_t, cth_objtoff
474*c42dbd0eSchristos@tab The offset of the data object symtypetab section, which maps ELF data symbols to
475*c42dbd0eSchristostypes.  @xref{The symtypetab sections}.
476*c42dbd0eSchristos
477*c42dbd0eSchristos@item 0x18
478*c42dbd0eSchristos@tab @code{uint32_t cth_funcoff}
479*c42dbd0eSchristos@vindex cth_funcoff
480*c42dbd0eSchristos@vindex struct ctf_header, cth_funcoff
481*c42dbd0eSchristos@vindex ctf_header_t, cth_funcoff
482*c42dbd0eSchristos@tab The offset of the function info symtypetab section, which maps ELF function
483*c42dbd0eSchristossymbols to a return type and arg types.  @xref{The symtypetab sections}.
484*c42dbd0eSchristos
485*c42dbd0eSchristos@item 0x1c
486*c42dbd0eSchristos@tab @code{uint32_t cth_objtidxoff}
487*c42dbd0eSchristos@vindex cth_objtidxoff
488*c42dbd0eSchristos@vindex struct ctf_header, cth_objtidxoff
489*c42dbd0eSchristos@vindex ctf_header_t, cth_objtidxoff
490*c42dbd0eSchristos@tab The offset of the object index section, which maps ELF object symbols to
491*c42dbd0eSchristosentries in the data object section.  @xref{The symtypetab sections}.
492*c42dbd0eSchristos
493*c42dbd0eSchristos@item 0x20
494*c42dbd0eSchristos@tab @code{uint32_t cth_funcidxoff}
495*c42dbd0eSchristos@vindex cth_funcidxoff
496*c42dbd0eSchristos@vindex struct ctf_header, cth_funcidxoff
497*c42dbd0eSchristos@vindex ctf_header_t, cth_funcidxoff
498*c42dbd0eSchristos@tab The offset of the function info index section, which maps ELF function
499*c42dbd0eSchristossymbols to entries in the function info section. @xref{The symtypetab sections}.
500*c42dbd0eSchristos
501*c42dbd0eSchristos@item 0x24
502*c42dbd0eSchristos@tab @code{uint32_t cth_varoff}
503*c42dbd0eSchristos@vindex cth_varoff
504*c42dbd0eSchristos@vindex struct ctf_header, cth_varoff
505*c42dbd0eSchristos@vindex ctf_header_t, cth_varoff
506*c42dbd0eSchristos@tab The offset of the variable section, which maps string names to types.
507*c42dbd0eSchristos@xref{The variable section}.
508*c42dbd0eSchristos
509*c42dbd0eSchristos@item 0x28
510*c42dbd0eSchristos@tab @code{uint32_t cth_typeoff}
511*c42dbd0eSchristos@vindex cth_typeoff
512*c42dbd0eSchristos@vindex struct ctf_header, cth_typeoff
513*c42dbd0eSchristos@vindex ctf_header_t, cth_typeoff
514*c42dbd0eSchristos@tab The offset of the type section, the core of CTF, which describes types
515*c42dbd0eSchristos using variable-length array elements. @xref{The type section}.
516*c42dbd0eSchristos
517*c42dbd0eSchristos@item 0x2c
518*c42dbd0eSchristos@tab @code{uint32_t cth_stroff}
519*c42dbd0eSchristos@vindex cth_stroff
520*c42dbd0eSchristos@vindex struct ctf_header, cth_stroff
521*c42dbd0eSchristos@vindex ctf_header_t, cth_stroff
522*c42dbd0eSchristos@tab The offset of the string section. @xref{The string section}.
523*c42dbd0eSchristos
524*c42dbd0eSchristos@item 0x30
525*c42dbd0eSchristos@tab @code{uint32_t cth_strlen}
526*c42dbd0eSchristos@vindex cth_strlen
527*c42dbd0eSchristos@vindex struct ctf_header, cth_strlen
528*c42dbd0eSchristos@vindex ctf_header_t, cth_strlen
529*c42dbd0eSchristos@tab The length of the string section (not an offset!).  The CTF file ends
530*c42dbd0eSchristosat this point.
531*c42dbd0eSchristos
532*c42dbd0eSchristos@end multitable
533*c42dbd0eSchristos
534*c42dbd0eSchristosEverything from this point on (until the end of the file at @code{cth_stroff} +
535*c42dbd0eSchristos@code{cth_strlen}) is compressed with zlib if @code{CTF_F_COMPRESS} is set in
536*c42dbd0eSchristosthe preamble's @code{ctp_flags}.
537*c42dbd0eSchristos
538*c42dbd0eSchristos@node The type section
539*c42dbd0eSchristos@section The type section
540*c42dbd0eSchristos@cindex Type section
541*c42dbd0eSchristos@cindex Sections, type
542*c42dbd0eSchristos
543*c42dbd0eSchristosThis section is the most important section in CTF, describing all the top-level
544*c42dbd0eSchristostypes in the program.  It consists of an array of type structures, each of which
545*c42dbd0eSchristosdescribes a type of some @dfn{kind}: each kind of type has some amount of
546*c42dbd0eSchristosvariable-length data associated with it (some kinds have none).  The amount of
547*c42dbd0eSchristosvariable-length data associated with a given type can be determined by
548*c42dbd0eSchristosinspecting the type, so the reading code can walk through the types in sequence
549*c42dbd0eSchristosat opening time.
550*c42dbd0eSchristos
551*c42dbd0eSchristosEach type structure is one of a set of overlapping structures in a discriminated
552*c42dbd0eSchristosunion of sorts: the variable-length data for each type immediately follows the
553*c42dbd0eSchristostype's type structure.  Here's the largest of the overlapping structures, which
554*c42dbd0eSchristosis only needed for huge types and so is very rarely seen:
555*c42dbd0eSchristos
556*c42dbd0eSchristos@verbatim
557*c42dbd0eSchristostypedef struct ctf_type
558*c42dbd0eSchristos{
559*c42dbd0eSchristos  uint32_t ctt_name;
560*c42dbd0eSchristos  uint32_t ctt_info;
561*c42dbd0eSchristos  __extension__
562*c42dbd0eSchristos  union
563*c42dbd0eSchristos  {
564*c42dbd0eSchristos    uint32_t ctt_size;
565*c42dbd0eSchristos    uint32_t ctt_type;
566*c42dbd0eSchristos  };
567*c42dbd0eSchristos  uint32_t ctt_lsizehi;
568*c42dbd0eSchristos  uint32_t ctt_lsizelo;
569*c42dbd0eSchristos} ctf_type_t;
570*c42dbd0eSchristos@end verbatim
571*c42dbd0eSchristos
572*c42dbd0eSchristosHere's the much more common smaller form:
573*c42dbd0eSchristos
574*c42dbd0eSchristos@verbatim
575*c42dbd0eSchristostypedef struct ctf_stype
576*c42dbd0eSchristos{
577*c42dbd0eSchristos  uint32_t ctt_name;
578*c42dbd0eSchristos  uint32_t ctt_info;
579*c42dbd0eSchristos  __extension__
580*c42dbd0eSchristos  union
581*c42dbd0eSchristos  {
582*c42dbd0eSchristos    uint32_t ctt_size;
583*c42dbd0eSchristos    uint32_t ctt_type;
584*c42dbd0eSchristos  };
585*c42dbd0eSchristos} ctf_type_t;
586*c42dbd0eSchristos@end verbatim
587*c42dbd0eSchristos
588*c42dbd0eSchristosIf @code{ctt_size} is the #define @code{CTF_LSIZE_SENT}, 0xffffffff, this type
589*c42dbd0eSchristosis described by a @code{ctf_type_t}: otherwise, a @code{ctf_stype_t}.
590*c42dbd0eSchristos@tindex CTF_LSIZE_SENT
591*c42dbd0eSchristos
592*c42dbd0eSchristosHere's what the fields mean:
593*c42dbd0eSchristos
594*c42dbd0eSchristos@tindex struct ctf_type
595*c42dbd0eSchristos@tindex struct ctf_stype
596*c42dbd0eSchristos@tindex ctf_type_t
597*c42dbd0eSchristos@tindex ctf_stype_t
598*c42dbd0eSchristos@multitable {0x1c (@code{ctf_type_t}} {@code{uint32_t ctt_lsizehi}} {The size of this type, if this type is of a kind for}
599*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
600*c42dbd0eSchristos@item 0x00
601*c42dbd0eSchristos@tab @code{uint32_t ctt_name}
602*c42dbd0eSchristos@vindex ctt_name
603*c42dbd0eSchristos@tab Strtab offset of the type name, if any (0 if none).
604*c42dbd0eSchristos
605*c42dbd0eSchristos@item 0x04
606*c42dbd0eSchristos@tab @code{uint32_t ctt_info}
607*c42dbd0eSchristos@vindex ctt_info
608*c42dbd0eSchristos@vindex struct ctf_type, ctt_info
609*c42dbd0eSchristos@vindex ctf_type_t, ctt_info
610*c42dbd0eSchristos@vindex struct ctf_stype, ctt_info
611*c42dbd0eSchristos@vindex ctf_stype_t, ctt_info
612*c42dbd0eSchristos@tab The @dfn{info word}, containing information on the kind of this type, its
613*c42dbd0eSchristosvariable-length data and whether it is visible to name lookup. See @xref{The
614*c42dbd0eSchristosinfo word}.
615*c42dbd0eSchristos
616*c42dbd0eSchristos@item 0x08
617*c42dbd0eSchristos@tab @code{uint32_t ctt_size}
618*c42dbd0eSchristos@vindex ctt_size
619*c42dbd0eSchristos@vindex struct ctf_type, ctt_size
620*c42dbd0eSchristos@vindex ctf_type_t, ctt_size
621*c42dbd0eSchristos@vindex struct ctf_stype, ctt_size
622*c42dbd0eSchristos@vindex ctf_stype_t, ctt_size
623*c42dbd0eSchristos@tab The size of this type, if this type is of a kind for which a size needs
624*c42dbd0eSchristosto be recorded (constant-size types don't need one).  If this is
625*c42dbd0eSchristos@code{CTF_LSIZE_SENT}, this type is a huge type described by @code{ctf_type_t}.
626*c42dbd0eSchristos
627*c42dbd0eSchristos@item 0x08
628*c42dbd0eSchristos@tab @code{uint32_t ctt_type}
629*c42dbd0eSchristos@vindex ctt_type
630*c42dbd0eSchristos@vindex struct ctf_stype, ctt_type
631*c42dbd0eSchristos@vindex ctf_stype_t, ctt_type
632*c42dbd0eSchristos@tab The type this type refers to, if this type is of a kind which refers to
633*c42dbd0eSchristosother types (like a pointer).  All such types are fixed-size, and no types that
634*c42dbd0eSchristosare variable-size refer to other types, so @code{ctt_size} and @code{ctt_type}
635*c42dbd0eSchristosoverlap.  All type kinds that use @code{ctt_type} are described by
636*c42dbd0eSchristos@code{ctf_stype_t}, not @code{ctf_type_t}. @xref{Type indexes and type IDs}.
637*c42dbd0eSchristos
638*c42dbd0eSchristos@item 0x0c (@code{ctf_type_t} only)
639*c42dbd0eSchristos@tab @code{uint32_t ctt_lsizehi}
640*c42dbd0eSchristos@vindex ctt_lsizehi
641*c42dbd0eSchristos@vindex struct ctf_type, ctt_lsizehi
642*c42dbd0eSchristos@vindex ctf_type_t, ctt_lsizehi
643*c42dbd0eSchristos@tab The high 32 bits of the size of a very large type. The @code{CTF_TYPE_LSIZE} macro
644*c42dbd0eSchristoscan be used to get a 64-bit size out of this field and the next one.
645*c42dbd0eSchristos@code{CTF_SIZE_TO_LSIZE_HI} splits the @code{ctt_lsizehi} out of it again.
646*c42dbd0eSchristos@findex CTF_TYPE_LSIZE
647*c42dbd0eSchristos@findex CTF_SIZE_TO_LSIZE_HI
648*c42dbd0eSchristos
649*c42dbd0eSchristos@item 0x10 (@code{ctf_type_t} only)
650*c42dbd0eSchristos@tab @code{uint32_t ctt_lsizelo}
651*c42dbd0eSchristos@vindex ctt_lsizelo
652*c42dbd0eSchristos@vindex struct ctf_type, ctt_lsizelo
653*c42dbd0eSchristos@vindex ctf_type_t, ctt_lsizelo
654*c42dbd0eSchristos@tab The low 32 bits of the size of a very large type.
655*c42dbd0eSchristos@code{CTF_SIZE_TO_LSIZE_LO} splits the @code{ctt_lsizelo} out of a 64-bit size.
656*c42dbd0eSchristos@findex CTF_SIZE_TO_LSIZE_LO
657*c42dbd0eSchristos@end multitable
658*c42dbd0eSchristos
659*c42dbd0eSchristosTwo aspects of this need further explanation: the info word, and what exactly a
660*c42dbd0eSchristostype ID is and how you determine it.  (Information on the various type-kind-
661*c42dbd0eSchristosdependent things, like whether @code{ctt_size} or @code{ctt_type} is used,
662*c42dbd0eSchristosis described in the section devoted to each kind.)
663*c42dbd0eSchristos
664*c42dbd0eSchristos@node The info word
665*c42dbd0eSchristos@subsection The info word, ctt_info
666*c42dbd0eSchristos
667*c42dbd0eSchristosThe info word is a bitfield split into three parts.  From MSB to LSB:
668*c42dbd0eSchristos
669*c42dbd0eSchristos@multitable {Bit offset} {@code{isroot}} {Length of variable-length data for this type (some kinds only).}
670*c42dbd0eSchristos@headitem Bit offset @tab Name @tab Description
671*c42dbd0eSchristos@item 26--31
672*c42dbd0eSchristos@tab @code{kind}
673*c42dbd0eSchristos@tab Type kind: @pxref{Type kinds}.
674*c42dbd0eSchristos
675*c42dbd0eSchristos@item 25
676*c42dbd0eSchristos@tab @code{isroot}
677*c42dbd0eSchristos@tab 1 if this type is visible to name lookup
678*c42dbd0eSchristos
679*c42dbd0eSchristos@item 0--24
680*c42dbd0eSchristos@tab @code{vlen}
681*c42dbd0eSchristos@tab Length of variable-length data for this type (some kinds only).
682*c42dbd0eSchristosThe variable-length data directly follows the @code{ctf_type_t} or
683*c42dbd0eSchristos@code{ctf_stype_t}.  This is a kind-dependent array length value,
684*c42dbd0eSchristosnot a length in bytes.  Some kinds have no variable-length data, or
685*c42dbd0eSchristosfixed-size variable-length data, and do not use this value.
686*c42dbd0eSchristos@end multitable
687*c42dbd0eSchristos
688*c42dbd0eSchristosThe most mysterious of these is undoubtedly @code{isroot}.  This indicates
689*c42dbd0eSchristoswhether types with names (nonzero @code{ctt_name}) are visible to name lookup:
690*c42dbd0eSchristosif zero, this type is considered a @dfn{non-root type} and you can't look it up
691*c42dbd0eSchristosby name at all.  Multiple types with the same name in the same C namespace
692*c42dbd0eSchristos(struct, union, enum, other) can exist in a single dictionary, but only one of
693*c42dbd0eSchristosthem may have a nonzero value for @code{isroot}.  @code{libctf} validates this
694*c42dbd0eSchristosat open time and refuses to open dictionaries that violate this constraint.
695*c42dbd0eSchristos
696*c42dbd0eSchristosHistorically, this feature was introduced for the encoding of bitfields
697*c42dbd0eSchristos(@pxref{Integer types}): for instance, int bitfields will all be named
698*c42dbd0eSchristos@code{int} with different widths or offsets, but only the full-width one at
699*c42dbd0eSchristosoffset zero is wanted when you look up the type named @code{int}.  With the
700*c42dbd0eSchristosintroduction of slices (@pxref{Slices}) as a more general bitfield encoding
701*c42dbd0eSchristosmechanism, this is less important, but we still use non-root types to handle
702*c42dbd0eSchristosconflicts if the linker API is used to fuse multiple translation units into one
703*c42dbd0eSchristosdictionary and those translation units contain types with the same name and
704*c42dbd0eSchristosconflicting definitions.  (We do not discuss this further here, because the
705*c42dbd0eSchristoslinker never does this: only specialized type mergers do, like that used for the
706*c42dbd0eSchristosLinux kernel.  The libctf documentation will describe this in more detail.)
707*c42dbd0eSchristos@c XXX update when libctf docs are written.
708*c42dbd0eSchristos
709*c42dbd0eSchristosThe @code{CTF_TYPE_INFO} macro can be used to compose an info word from
710*c42dbd0eSchristosa @code{kind}, @code{isroot}, and @code{vlen}; @code{CTF_V2_INFO_KIND},
711*c42dbd0eSchristos@code{CTF_V2_INFO_ISROOT} and @code{CTF_V2_INFO_VLEN} pick it apart again.
712*c42dbd0eSchristos@findex CTF_TYPE_INFO
713*c42dbd0eSchristos@findex CTF_V2_INFO_KIND
714*c42dbd0eSchristos@findex CTF_V2_INFO_ISROOT
715*c42dbd0eSchristos@findex CTF_V2_INFO_VLEN
716*c42dbd0eSchristos
717*c42dbd0eSchristos@node Type indexes and type IDs
718*c42dbd0eSchristos@subsection Type indexes and type IDs
719*c42dbd0eSchristos@cindex Type indexes
720*c42dbd0eSchristos@cindex Type IDs
721*c42dbd0eSchristos@cindex Type, IDs of
722*c42dbd0eSchristos@cindex Type, indexes of
723*c42dbd0eSchristos@cindex ctf_id_t
724*c42dbd0eSchristos
725*c42dbd0eSchristos@cindex Parent range
726*c42dbd0eSchristos@cindex Child range
727*c42dbd0eSchristos@cindex Type IDs, ranges
728*c42dbd0eSchristosTypes are referred to within the CTF file via @dfn{type IDs}.  A type ID is a
729*c42dbd0eSchristosnumber from 0 to @math{2^32}, from a space divided in half.  Types @math{2^31-1}
730*c42dbd0eSchristosand below are in the @dfn{parent range}: these IDs are used for dictionaries
731*c42dbd0eSchristosthat have not had any other dictionary @code{ctf_import}ed into it as a parent.
732*c42dbd0eSchristosBoth completely standalone dictionaries and parent dictionaries with children
733*c42dbd0eSchristoshanging off them have types in this range.  Types @math{2^31} and above are in
734*c42dbd0eSchristosthe @dfn{child range}: only types in child dictionaries are in this range.
735*c42dbd0eSchristos
736*c42dbd0eSchristosThese IDs appear in @code{ctf_type_t.ctt_type} (@pxref{The type section}), but
737*c42dbd0eSchristosthe types themselves have no visible ID: quite intentionally, because adding an
738*c42dbd0eSchristosID uses space, and every ID is different so they don't compress well.  The IDs
739*c42dbd0eSchristosare implicit: at open time, the consumer walks through the entire type section
740*c42dbd0eSchristosand counts the types in the type section. The type section is an array of
741*c42dbd0eSchristosvariable-length elements, so each entry could be considered as having an index,
742*c42dbd0eSchristosstarting from 1. We count these indexes and associate each with its
743*c42dbd0eSchristoscorresponding @code{ctf_type_t} or @code{ctf_stype_t}.
744*c42dbd0eSchristos
745*c42dbd0eSchristosLookups of types with IDs in the parent space look in the parent dictionary if
746*c42dbd0eSchristosthis dictionary has one associated with it; lookups of types with IDs in the
747*c42dbd0eSchristoschild space error out if the dictionary does not have a parent, and otherwise
748*c42dbd0eSchristosconvert the ID into an index by shaving off the top bit and look up the index
749*c42dbd0eSchristosin the child.
750*c42dbd0eSchristos
751*c42dbd0eSchristosThese properties mean that the same dictionary can be used as a parent of child
752*c42dbd0eSchristosdictionaries and can also be used directly with no children at all, but a
753*c42dbd0eSchristosdictionary created as a child dictionary must always be associated with a parent
754*c42dbd0eSchristos--- usually, the same parent --- because its references to its own types have
755*c42dbd0eSchristosthe high bit turned on and this is only flipped off again if this is a child
756*c42dbd0eSchristosdictionary.  (This is not a problem, because if you @emph{don't} associate the
757*c42dbd0eSchristoschild with a parent, any references within it to its parent types will fail, and
758*c42dbd0eSchristosthere are almost certain to be many such references, or why is it a child at
759*c42dbd0eSchristosall?)
760*c42dbd0eSchristos
761*c42dbd0eSchristosThis does mean that consumers should keep a close eye on the distinction between
762*c42dbd0eSchristostype IDs and type indexes: if you mix them up, everything will appear to work as
763*c42dbd0eSchristoslong as you're only using parent dictionaries or standalone dictionaries, but as
764*c42dbd0eSchristossoon as you start using children, everything will fail horribly.
765*c42dbd0eSchristos
766*c42dbd0eSchristosType index zero, and type ID zero, are used to indicate that this type cannot be
767*c42dbd0eSchristosrepresented in CTF as currently constituted: they are emitted by the compiler,
768*c42dbd0eSchristosbut all type chains that terminate in the unknown type are erased at link time
769*c42dbd0eSchristos(structure fields that use them just vanish, etc).  So you will probably never
770*c42dbd0eSchristossee a use of type zero outside the symtypetab sections, where they serve as
771*c42dbd0eSchristossentinels of sorts, to indicate symbols with no associated type.
772*c42dbd0eSchristos
773*c42dbd0eSchristosThe macros @code{CTF_V2_TYPE_TO_INDEX} and @code{CTF_V2_INDEX_TO_TYPE} may help
774*c42dbd0eSchristosin translation between types and indexes: @code{CTF_V2_TYPE_ISPARENT} and
775*c42dbd0eSchristos@code{CTF_V2_TYPE_ISCHILD} can be used to tell whether a given ID is in the
776*c42dbd0eSchristosparent or child range.
777*c42dbd0eSchristos@findex CTF_V2_TYPE_TO_INDEX
778*c42dbd0eSchristos@findex CTF_V2_INDEX_TO_TYPE
779*c42dbd0eSchristos@findex CTF_V2_TYPE_ISPARENT
780*c42dbd0eSchristos@findex CTF_V2_TYPE_ISCHILD
781*c42dbd0eSchristos
782*c42dbd0eSchristosIt is quite possible and indeed common for type IDs to point forward in the
783*c42dbd0eSchristosdictionary, as well as backward.
784*c42dbd0eSchristos
785*c42dbd0eSchristos@node Type kinds
786*c42dbd0eSchristos@subsection Type kinds
787*c42dbd0eSchristos@cindex Type kinds
788*c42dbd0eSchristos@cindex Type, kinds of
789*c42dbd0eSchristos
790*c42dbd0eSchristosEvery type in CTF is of some @dfn{kind}. Each kind is some variety of C type:
791*c42dbd0eSchristosall structures are a single kind, as are all unions, all pointers, all arrays,
792*c42dbd0eSchristosall integers regardless of their bitfield width, etc.  The kind of a type is
793*c42dbd0eSchristosgiven in the @code{kind} field of the @code{ctt_info} word (@pxref{The info
794*c42dbd0eSchristosword}).
795*c42dbd0eSchristos
796*c42dbd0eSchristosThe space of type kinds is only a quarter full so far, so there is plenty of
797*c42dbd0eSchristosroom for expansion.  It is likely that in future versions of the file format,
798*c42dbd0eSchristostypes with smaller kinds will be more efficiently encoded than types with larger
799*c42dbd0eSchristoskinds, so their numerical value will actually start to matter in future. (So
800*c42dbd0eSchristosthese IDs will probably change their numerical values in a later release of this
801*c42dbd0eSchristosformat, to move more frequently-used kinds like structures and cv-quals towards
802*c42dbd0eSchristosthe top of the space, and move rarely-used kinds like integers downwards. Yes,
803*c42dbd0eSchristosintegers are rare: how many kinds of @code{int} are there in a program? They're
804*c42dbd0eSchristosjust very frequently @emph{referenced}.)
805*c42dbd0eSchristos
806*c42dbd0eSchristosHere's the set of kinds so far.  Each kind has a @code{#define} associated with
807*c42dbd0eSchristosit, also given here.
808*c42dbd0eSchristos
809*c42dbd0eSchristos@multitable {Kind} {@code{CTF_K_VOLATILE}} {Indicates a type that cannot be represented in CTF, or that} {@xref{Pointers typedefs and cvr-quals}}
810*c42dbd0eSchristos@headitem Kind @tab Macro @tab Purpose
811*c42dbd0eSchristos@item 0
812*c42dbd0eSchristos@tab @code{CTF_K_UNKNOWN}
813*c42dbd0eSchristos@tab Indicates a type that cannot be represented in CTF, or that is being skipped.
814*c42dbd0eSchristosIt is very similar to type ID 0, except that you can have @emph{multiple}, distinct types
815*c42dbd0eSchristosof kind @code{CTF_K_UNKNOWN}.
816*c42dbd0eSchristos@tindex CTF_K_UNKNOWN
817*c42dbd0eSchristos
818*c42dbd0eSchristos@item 1
819*c42dbd0eSchristos@tab @code{CTF_K_INTEGER}
820*c42dbd0eSchristos@tab An integer type. @xref{Integer types}.
821*c42dbd0eSchristos
822*c42dbd0eSchristos@item 2
823*c42dbd0eSchristos@tab @code{CTF_K_FLOAT}
824*c42dbd0eSchristos@tab A floating-point type. @xref{Floating-point types}.
825*c42dbd0eSchristos
826*c42dbd0eSchristos@item 3
827*c42dbd0eSchristos@tab @code{CTF_K_POINTER}
828*c42dbd0eSchristos@tab A pointer. @xref{Pointers typedefs and cvr-quals}.
829*c42dbd0eSchristos
830*c42dbd0eSchristos@item 4
831*c42dbd0eSchristos@tab @code{CTF_K_ARRAY}
832*c42dbd0eSchristos@tab An array. @xref{Arrays}.
833*c42dbd0eSchristos
834*c42dbd0eSchristos@item 5
835*c42dbd0eSchristos@tab @code{CTF_K_FUNCTION}
836*c42dbd0eSchristos@tab A function pointer. @xref{Function pointers}.
837*c42dbd0eSchristos
838*c42dbd0eSchristos@item 6
839*c42dbd0eSchristos@tab @code{CTF_K_STRUCT}
840*c42dbd0eSchristos@tab A structure. @xref{Structs and unions}.
841*c42dbd0eSchristos
842*c42dbd0eSchristos@item 7
843*c42dbd0eSchristos@tab @code{CTF_K_UNION}
844*c42dbd0eSchristos@tab A union. @xref{Structs and unions}.
845*c42dbd0eSchristos
846*c42dbd0eSchristos@item 8
847*c42dbd0eSchristos@tab @code{CTF_K_ENUM}
848*c42dbd0eSchristos@tab An enumerated type. @xref{Enums}.
849*c42dbd0eSchristos
850*c42dbd0eSchristos@item 9
851*c42dbd0eSchristos@tab @code{CTF_K_FORWARD}
852*c42dbd0eSchristos@tab A forward. @xref{Forward declarations}.
853*c42dbd0eSchristos
854*c42dbd0eSchristos@item 10
855*c42dbd0eSchristos@tab @code{CTF_K_TYPEDEF}
856*c42dbd0eSchristos@tab A typedef. @xref{Pointers typedefs and cvr-quals}.
857*c42dbd0eSchristos
858*c42dbd0eSchristos@item 11
859*c42dbd0eSchristos@tab @code{CTF_K_VOLATILE}
860*c42dbd0eSchristos@tab A volatile-qualified type. @xref{Pointers typedefs and cvr-quals}.
861*c42dbd0eSchristos
862*c42dbd0eSchristos@item 12
863*c42dbd0eSchristos@tab @code{CTF_K_CONST}
864*c42dbd0eSchristos@tab A const-qualified type. @xref{Pointers typedefs and cvr-quals}.
865*c42dbd0eSchristos
866*c42dbd0eSchristos@item 13
867*c42dbd0eSchristos@tab @code{CTF_K_RESTRICT}
868*c42dbd0eSchristos@tab A restrict-qualified type. @xref{Pointers typedefs and cvr-quals}.
869*c42dbd0eSchristos
870*c42dbd0eSchristos@item 14
871*c42dbd0eSchristos@tab @code{CTF_K_SLICE}
872*c42dbd0eSchristos@tab A slice, a change of the bit-width or offset of some other type. @xref{Slices}.
873*c42dbd0eSchristos@end multitable
874*c42dbd0eSchristos
875*c42dbd0eSchristosNow we cover all type kinds in turn. Some are more complicated than others.
876*c42dbd0eSchristos
877*c42dbd0eSchristos@node Integer types
878*c42dbd0eSchristos@subsection Integer types
879*c42dbd0eSchristos@cindex Integer types
880*c42dbd0eSchristos@cindex Types, integer
881*c42dbd0eSchristos@tindex int
882*c42dbd0eSchristos@tindex long
883*c42dbd0eSchristos@tindex long long
884*c42dbd0eSchristos@tindex short
885*c42dbd0eSchristos@tindex char
886*c42dbd0eSchristos@tindex bool
887*c42dbd0eSchristos@tindex unsigned int
888*c42dbd0eSchristos@tindex unsigned long
889*c42dbd0eSchristos@tindex unsigned long long
890*c42dbd0eSchristos@tindex unsigned short
891*c42dbd0eSchristos@tindex unsigned char
892*c42dbd0eSchristos@tindex signed int
893*c42dbd0eSchristos@tindex signed long
894*c42dbd0eSchristos@tindex signed long long
895*c42dbd0eSchristos@tindex signed short
896*c42dbd0eSchristos@tindex signed char
897*c42dbd0eSchristos@cindex CTF_K_INTEGER
898*c42dbd0eSchristos
899*c42dbd0eSchristosIntegral types are all represented as types of kind @code{CTF_K_INTEGER}.  These
900*c42dbd0eSchristostypes fill out @code{ctt_size} in the @code{ctf_stype_t} with the size in bytes
901*c42dbd0eSchristosof the integral type in question.  They are always represented by
902*c42dbd0eSchristos@code{ctf_stype_t}, never @code{ctf_type_t}. Their variable-length data is one
903*c42dbd0eSchristos@code{uint32_t} in length: @code{vlen} in the info word should be disregarded
904*c42dbd0eSchristosand is always zero.
905*c42dbd0eSchristos
906*c42dbd0eSchristosThe variable-length data for integers has multiple items packed into it much
907*c42dbd0eSchristoslike the info word does.
908*c42dbd0eSchristos
909*c42dbd0eSchristos@multitable {Bit offset} {Encoding} {The integer encoding and desired display representation.}
910*c42dbd0eSchristos@headitem Bit offset @tab Name @tab Description
911*c42dbd0eSchristos@item 24--31
912*c42dbd0eSchristos@tab Encoding
913*c42dbd0eSchristos@tab The desired display representation of this integer. You can extract this
914*c42dbd0eSchristosfield with the @code{CTF_INT_ENCODING} macro.  See below.
915*c42dbd0eSchristos@findex CTF_INT_ENCODING
916*c42dbd0eSchristos
917*c42dbd0eSchristos@item 16--23
918*c42dbd0eSchristos@tab Offset
919*c42dbd0eSchristos@tab The offset of this integral type in bits from the start of its enclosing
920*c42dbd0eSchristosstructure field, adjusted for endianness: @pxref{Structs and unions}.  You can
921*c42dbd0eSchristosextract this field with the @code{CTF_INT_OFFSET} macro.
922*c42dbd0eSchristos@findex CTF_INT_OFFSET
923*c42dbd0eSchristos
924*c42dbd0eSchristos@item 0--15
925*c42dbd0eSchristos@tab Bit-width
926*c42dbd0eSchristos@tab The width of this integral type in bits.  You can extract this field with
927*c42dbd0eSchristosthe @code{CTF_INT_BITS} macro.
928*c42dbd0eSchristos@findex CTF_INT_BITS
929*c42dbd0eSchristos@end multitable
930*c42dbd0eSchristos
931*c42dbd0eSchristosIf you choose, bitfields can be represented using the things above as a sort of
932*c42dbd0eSchristosintegral type with the @code{isroot} bit flipped off and the offset and bits
933*c42dbd0eSchristosvalues set in the vlen word: you can populate it with the @code{CTF_INT_DATA}
934*c42dbd0eSchristosmacro.  (But it may be more convenient to represent them using slices of a
935*c42dbd0eSchristosfull-width integer: @pxref{Slices}.)
936*c42dbd0eSchristos@findex CTF_INT_DATA
937*c42dbd0eSchristos
938*c42dbd0eSchristosIntegers that are bitfields usually have a @code{ctt_size} rounded up to the
939*c42dbd0eSchristosnearest power of two in bytes, for natural alignment (e.g. a 17-bit integer
940*c42dbd0eSchristoswould have a @code{ctt_size} of 4).  However, not all types are naturally
941*c42dbd0eSchristosaligned on all architectures: packed structures may in theory use integral
942*c42dbd0eSchristosbitfields with different @code{ctt_size}, though this is rarely observed.
943*c42dbd0eSchristos
944*c42dbd0eSchristosThe @dfn{encoding} for integers is a bit-field comprised of the values below,
945*c42dbd0eSchristoswhich consumers can use to decide how to display values of this type:
946*c42dbd0eSchristos
947*c42dbd0eSchristos@multitable {Offset} {@code{CTF_INT_VARARGS}} {If set, this is a char type.  It is platform-dependent whether unadorned}
948*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
949*c42dbd0eSchristos@item 0x01
950*c42dbd0eSchristos@tab @code{CTF_INT_SIGNED}
951*c42dbd0eSchristos@tab If set, this is a signed int: if false, unsigned.
952*c42dbd0eSchristos@tindex CTF_INT_SIGNED
953*c42dbd0eSchristos
954*c42dbd0eSchristos@item 0x02
955*c42dbd0eSchristos@tab @code{CTF_INT_CHAR}
956*c42dbd0eSchristos@tab If set, this is a char type.  It is platform-dependent whether unadorned
957*c42dbd0eSchristos@code{char} is signed or not: the @code{CTF_CHAR} macro produces an integral
958*c42dbd0eSchristostype suitable for the definition of @code{char} on this platform.
959*c42dbd0eSchristos@tindex CTF_INT_CHAR
960*c42dbd0eSchristos@findex CTF_CHAR
961*c42dbd0eSchristos
962*c42dbd0eSchristos@item 0x04
963*c42dbd0eSchristos@tab @code{CTF_INT_BOOL}
964*c42dbd0eSchristos@tab If set, this is a boolean type.  (It is theoretically possible to turn this
965*c42dbd0eSchristosand @code{CTF_INT_CHAR} on at the same time, but it is not clear what this would
966*c42dbd0eSchristosmean.)
967*c42dbd0eSchristos@tindex CTF_INT_BOOL
968*c42dbd0eSchristos
969*c42dbd0eSchristos@item 0x08
970*c42dbd0eSchristos@tab @code{CTF_INT_VARARGS}
971*c42dbd0eSchristos@tab If set, this is a varargs-promoted value in a K&R function definition.
972*c42dbd0eSchristosThis is not currently produced or consumed by anything that we know of: it is set
973*c42dbd0eSchristosaside for future use.
974*c42dbd0eSchristos@end multitable
975*c42dbd0eSchristos
976*c42dbd0eSchristosThe GCC ``@code{Complex int}'' and fixed-point extensions are not yet supported:
977*c42dbd0eSchristosreferences to such types will be emitted as type 0.
978*c42dbd0eSchristos
979*c42dbd0eSchristos@node Floating-point types
980*c42dbd0eSchristos@subsection Floating-point types
981*c42dbd0eSchristos@cindex Floating-point types
982*c42dbd0eSchristos@cindex Types, floating-point
983*c42dbd0eSchristos@tindex float
984*c42dbd0eSchristos@tindex double
985*c42dbd0eSchristos@tindex signed float
986*c42dbd0eSchristos@tindex signed double
987*c42dbd0eSchristos@tindex unsigned float
988*c42dbd0eSchristos@tindex unsigned double
989*c42dbd0eSchristos@tindex Complex, float
990*c42dbd0eSchristos@tindex Complex, double
991*c42dbd0eSchristos@tindex Complex, signed float
992*c42dbd0eSchristos@tindex Complex, signed double
993*c42dbd0eSchristos@tindex Complex, unsigned float
994*c42dbd0eSchristos@tindex Complex, unsigned double
995*c42dbd0eSchristos@cindex CTF_K_FLOAT
996*c42dbd0eSchristos
997*c42dbd0eSchristosFloating-point types are all represented as types of kind @code{CTF_K_FLOAT}.
998*c42dbd0eSchristosLike integers, These types fill out @code{ctt_size} in the @code{ctf_stype_t}
999*c42dbd0eSchristoswith the size in bytes of the floating-point type in question.  They are always
1000*c42dbd0eSchristosrepresented by @code{ctf_stype_t}, never @code{ctf_type_t}.
1001*c42dbd0eSchristos
1002*c42dbd0eSchristosThis part of CTF shows many rough edges in the more obscure corners of
1003*c42dbd0eSchristosfloating-point handling, and is likely to change in format v4.
1004*c42dbd0eSchristos
1005*c42dbd0eSchristosThe variable-length data for floats has multiple items packed into it just like
1006*c42dbd0eSchristosintegers do:
1007*c42dbd0eSchristos
1008*c42dbd0eSchristos@multitable {Bit offset} {Encoding} {The floating-;point encoding and desired display representation.}
1009*c42dbd0eSchristos@headitem Bit offset @tab Name @tab Description
1010*c42dbd0eSchristos@item 24--31
1011*c42dbd0eSchristos@tab Encoding
1012*c42dbd0eSchristos@tab The desired display representation of this float. You can extract this
1013*c42dbd0eSchristosfield with the @code{CTF_FP_ENCODING} macro.  See below.
1014*c42dbd0eSchristos@findex CTF_FP_ENCODING
1015*c42dbd0eSchristos
1016*c42dbd0eSchristos@item 16--23
1017*c42dbd0eSchristos@tab Offset
1018*c42dbd0eSchristos@tab The offset of this floating-point type in bits from the start of its enclosing
1019*c42dbd0eSchristosstructure field, adjusted for endianness: @pxref{Structs and unions}.  You can
1020*c42dbd0eSchristosextract this field with the @code{CTF_FP_OFFSET} macro.
1021*c42dbd0eSchristos@findex CTF_FP_OFFSET
1022*c42dbd0eSchristos
1023*c42dbd0eSchristos@item 0--15
1024*c42dbd0eSchristos@tab Bit-width
1025*c42dbd0eSchristos@tab The width of this floating-point type in bits.  You can extract this field with
1026*c42dbd0eSchristosthe @code{CTF_FP_BITS} macro.
1027*c42dbd0eSchristos@findex CTF_FP_BITS
1028*c42dbd0eSchristos@end multitable
1029*c42dbd0eSchristos
1030*c42dbd0eSchristosThe purpose of the floating-point offset and bit-width is somewhat opaque, since
1031*c42dbd0eSchristosthere are no such things as floating-point bitfields in C: the bit-width should
1032*c42dbd0eSchristosbe filled out with the full width of the type in bits, and the offset should
1033*c42dbd0eSchristosalways be zero.  It is likely that these fields will go away in the future.  As
1034*c42dbd0eSchristoswith integers, you can use @code{CTF_FP_DATA} to assemble one of these vlen
1035*c42dbd0eSchristositems from its component parts.
1036*c42dbd0eSchristos@findex CTF_INT_DATA
1037*c42dbd0eSchristos
1038*c42dbd0eSchristosThe @dfn{encoding} for floats is not a bitfield but a simple value indicating
1039*c42dbd0eSchristosthe display representation.  Many of these are unused, relate to
1040*c42dbd0eSchristosSolaris-specific compiler extensions, and will be recycled in future: some are
1041*c42dbd0eSchristosunused and will become used in future.
1042*c42dbd0eSchristos
1043*c42dbd0eSchristos@multitable {Offset} {@code{CTF_FP_LDIMAGRY}} {This is a @code{float} interval type, a Solaris-specific extension.}
1044*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1045*c42dbd0eSchristos@item 1
1046*c42dbd0eSchristos@tab @code{CTF_FP_SINGLE}
1047*c42dbd0eSchristos@tab This is a single-precision IEEE 754 @code{float}.
1048*c42dbd0eSchristos@tindex CTF_FP_SINGLE
1049*c42dbd0eSchristos@item 2
1050*c42dbd0eSchristos@tab @code{CTF_FP_DOUBLE}
1051*c42dbd0eSchristos@tab This is a double-precision IEEE 754 @code{double}.
1052*c42dbd0eSchristos@tindex CTF_FP_DOUBLE
1053*c42dbd0eSchristos@item 3
1054*c42dbd0eSchristos@tab @code{CTF_FP_CPLX}
1055*c42dbd0eSchristos@tab This is a @code{Complex float}.
1056*c42dbd0eSchristos@tindex CTF_FP_CPLX
1057*c42dbd0eSchristos@item 4
1058*c42dbd0eSchristos@tab @code{CTF_FP_DCPLX}
1059*c42dbd0eSchristos@tab This is a @code{Complex double}.
1060*c42dbd0eSchristos@tindex CTF_FP_DCPLX
1061*c42dbd0eSchristos@item 5
1062*c42dbd0eSchristos@tab @code{CTF_FP_LDCPLX}
1063*c42dbd0eSchristos@tab This is a @code{Complex long double}.
1064*c42dbd0eSchristos@tindex CTF_FP_LDCPLX
1065*c42dbd0eSchristos@item 6
1066*c42dbd0eSchristos@tab @code{CTF_FP_LDOUBLE}
1067*c42dbd0eSchristos@tab This is a @code{long double}.
1068*c42dbd0eSchristos@tindex CTF_FP_LDOUBLE
1069*c42dbd0eSchristos@item 7
1070*c42dbd0eSchristos@tab @code{CTF_FP_INTRVL}
1071*c42dbd0eSchristos@tab This is a @code{float} interval type, a Solaris-specific extension.
1072*c42dbd0eSchristosUnused: will be recycled.
1073*c42dbd0eSchristos@tindex CTF_FP_INTRVL
1074*c42dbd0eSchristos@cindex Unused bits
1075*c42dbd0eSchristos@item 8
1076*c42dbd0eSchristos@tab @code{CTF_FP_DINTRVL}
1077*c42dbd0eSchristos@tab This is a @code{double} interval type, a Solaris-specific extension.
1078*c42dbd0eSchristosUnused: will be recycled.
1079*c42dbd0eSchristos@tindex CTF_FP_DINTRVL
1080*c42dbd0eSchristos@cindex Unused bits
1081*c42dbd0eSchristos@item 9
1082*c42dbd0eSchristos@tab @code{CTF_FP_LDINTRVL}
1083*c42dbd0eSchristos@tab This is a @code{long double} interval type, a Solaris-specific extension.
1084*c42dbd0eSchristosUnused: will be recycled.
1085*c42dbd0eSchristos@tindex CTF_FP_LDINTRVL
1086*c42dbd0eSchristos@cindex Unused bits
1087*c42dbd0eSchristos@item 10
1088*c42dbd0eSchristos@tab @code{CTF_FP_IMAGRY}
1089*c42dbd0eSchristos@tab This is a the imaginary part of a @code{Complex float}. Not currently
1090*c42dbd0eSchristosgenerated. May change.
1091*c42dbd0eSchristos@tindex CTF_FP_IMAGRY
1092*c42dbd0eSchristos@cindex Unused bits
1093*c42dbd0eSchristos@item 11
1094*c42dbd0eSchristos@tab @code{CTF_FP_DIMAGRY}
1095*c42dbd0eSchristos@tab This is a the imaginary part of a @code{Complex double}. Not currently
1096*c42dbd0eSchristosgenerated. May change.
1097*c42dbd0eSchristos@tindex CTF_FP_DIMAGRY
1098*c42dbd0eSchristos@cindex Unused bits
1099*c42dbd0eSchristos@item 12
1100*c42dbd0eSchristos@tab @code{CTF_FP_LDIMAGRY}
1101*c42dbd0eSchristos@tab This is a the imaginary part of a @code{Complex long double}. Not currently
1102*c42dbd0eSchristosgenerated. May change.
1103*c42dbd0eSchristos@tindex CTF_FP_LDIMAGRY
1104*c42dbd0eSchristos@cindex Unused bits
1105*c42dbd0eSchristos@end multitable
1106*c42dbd0eSchristos
1107*c42dbd0eSchristosThe use of the complex floating-point encodings is obscure: it is possible that
1108*c42dbd0eSchristos@code{CTF_FP_CPLX} is meant to be used for only the real part of complex types,
1109*c42dbd0eSchristosand @code{CTF_FP_IMAGRY} et al for the imaginary part -- but for now, we are
1110*c42dbd0eSchristosemitting @code{CTF_FP_CPLX} to cover the entire type, with no way to get at its
1111*c42dbd0eSchristosconstituent parts. There appear to be no uses of these encodings anywhere, so
1112*c42dbd0eSchristosthey are quite likely to change incompatibly in future.
1113*c42dbd0eSchristos
1114*c42dbd0eSchristos@node Slices
1115*c42dbd0eSchristos@subsection Slices
1116*c42dbd0eSchristos@cindex Slices
1117*c42dbd0eSchristos@cindex Types, slices of integral
1118*c42dbd0eSchristos@tindex CTF_K_SLICE
1119*c42dbd0eSchristos
1120*c42dbd0eSchristosSlices, with kind @code{CTF_K_SLICE}, are an unusual CTF construct: they do not
1121*c42dbd0eSchristosdirectly correspond to any C type, but are a way to model other types in a more
1122*c42dbd0eSchristosconvenient fashion for CTF generators.
1123*c42dbd0eSchristos
1124*c42dbd0eSchristosA slice is like a pointer or other reference type in that they are always
1125*c42dbd0eSchristosrepresented by @code{ctf_stype_t}: but unlike pointers and other reference
1126*c42dbd0eSchristostypes, they populate the @code{ctt_size} field just like integral types do, and
1127*c42dbd0eSchristoscome with an attached encoding and transform the encoding of the underlying
1128*c42dbd0eSchristostype.  The underlying type is described in the variable-length data, similarly
1129*c42dbd0eSchristosto structure and union fields: see below.  Requests for the type size should
1130*c42dbd0eSchristosalso chase down to the referenced type.
1131*c42dbd0eSchristos
1132*c42dbd0eSchristosSlices are always nameless: @code{ctt_name} is always zero for them.
1133*c42dbd0eSchristos
1134*c42dbd0eSchristos(The @code{libctf} API behaviour is unusual as well, and justifies the existence
1135*c42dbd0eSchristosof slices: @code{ctf_type_kind} never returns @code{CTF_K_SLICE} but always the
1136*c42dbd0eSchristosunderlying type kind, so that consumers never need to know about slices: they
1137*c42dbd0eSchristoscan tell if an apparent integer is actually a slice if they need to by calling
1138*c42dbd0eSchristos@code{ctf_type_reference}, which will uniquely return the underlying integral
1139*c42dbd0eSchristostype rather than erroring out with @code{ECTF_NOTREF} if this is actually a
1140*c42dbd0eSchristosslice. So slices act just like an integer with an encoding, but more closely
1141*c42dbd0eSchristosmirror DWARF and other debugging information formats by allowing CTF file
1142*c42dbd0eSchristoscreators to represent a bitfield as a slice of an underlying integral type.)
1143*c42dbd0eSchristos@findex Slices, effect on ctf_type_kind
1144*c42dbd0eSchristos@findex Slices, effect on ctf_type_reference
1145*c42dbd0eSchristos@findex libctf, effect of slices
1146*c42dbd0eSchristos
1147*c42dbd0eSchristosThe vlen in the info word for a slice should be ignored and is always zero. The
1148*c42dbd0eSchristosvariable-length data for a slice is a single @code{ctf_slice_t}:
1149*c42dbd0eSchristos
1150*c42dbd0eSchristos@verbatim
1151*c42dbd0eSchristostypedef struct ctf_slice
1152*c42dbd0eSchristos{
1153*c42dbd0eSchristos  uint32_t cts_type;
1154*c42dbd0eSchristos  unsigned short cts_offset;
1155*c42dbd0eSchristos  unsigned short cts_bits;
1156*c42dbd0eSchristos} ctf_slice_t;
1157*c42dbd0eSchristos@end verbatim
1158*c42dbd0eSchristos
1159*c42dbd0eSchristos@tindex struct ctf_slice
1160*c42dbd0eSchristos@tindex ctf_slice_t
1161*c42dbd0eSchristos@multitable {Offset} {@code{unsigned short cts_offset}} {The type this slice is a slice of.  Must be an}
1162*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1163*c42dbd0eSchristos@item 0x0
1164*c42dbd0eSchristos@tab @code{uint32_t cts_type}
1165*c42dbd0eSchristos@vindex cts_type
1166*c42dbd0eSchristos@vindex struct ctf_slice, cts_type
1167*c42dbd0eSchristos@vindex ctf_slice_t, cts_type
1168*c42dbd0eSchristos@tab The type this slice is a slice of.  Must be an integral type (or a
1169*c42dbd0eSchristosfloating-point type, but this nonsensical option will go away in v4.)
1170*c42dbd0eSchristos
1171*c42dbd0eSchristos@item 0x4
1172*c42dbd0eSchristos@tab @code{unsigned short cts_offset}
1173*c42dbd0eSchristos@vindex cts_offset
1174*c42dbd0eSchristos@vindex struct ctf_slice, cts_offset
1175*c42dbd0eSchristos@vindex ctf_slice_t, cts_offset
1176*c42dbd0eSchristos@tab The offset of this integral type in bits from the start of its enclosing
1177*c42dbd0eSchristosstructure field, adjusted for endianness: @pxref{Structs and unions}. Identical
1178*c42dbd0eSchristossemantics to the @code{CTF_INT_OFFSET} field: @pxref{Integer types}.  This field
1179*c42dbd0eSchristosis much too long, because the maximum possible offset of an integral type would
1180*c42dbd0eSchristoseasily fit in a char: this field is bigger just for the sake of alignment.  This
1181*c42dbd0eSchristoswill change in v4.
1182*c42dbd0eSchristos
1183*c42dbd0eSchristos@item 0x6
1184*c42dbd0eSchristos@tab @code{unsigned short cts_bits}
1185*c42dbd0eSchristos@vindex cts_bits
1186*c42dbd0eSchristos@vindex struct ctf_slice, cts_bits
1187*c42dbd0eSchristos@vindex ctf_slice_t, cts_bits
1188*c42dbd0eSchristos@tab The bit-width of this integral type. Identical semantics to the
1189*c42dbd0eSchristos@code{CTF_INT_BITS} field: @pxref{Integer types}.  As above, this field is
1190*c42dbd0eSchristosreally too large and will shrink in v4.
1191*c42dbd0eSchristos@end multitable
1192*c42dbd0eSchristos
1193*c42dbd0eSchristos@node Pointers typedefs and cvr-quals
1194*c42dbd0eSchristos@subsection Pointers, typedefs, and cvr-quals
1195*c42dbd0eSchristos@cindex Pointers
1196*c42dbd0eSchristos@cindex Typedefs
1197*c42dbd0eSchristos@cindex cvr-quals
1198*c42dbd0eSchristos@tindex typedef
1199*c42dbd0eSchristos@tindex const
1200*c42dbd0eSchristos@tindex volatile
1201*c42dbd0eSchristos@tindex restrict
1202*c42dbd0eSchristos@tindex CTF_K_POINTER
1203*c42dbd0eSchristos@tindex CTF_K_TYPEDEF
1204*c42dbd0eSchristos@tindex CTF_K_CONST
1205*c42dbd0eSchristos@tindex CTF_K_VOLATILE
1206*c42dbd0eSchristos@tindex CTF_K_RESTRICT
1207*c42dbd0eSchristos
1208*c42dbd0eSchristosPointers, @code{typedef}s, and @code{const}, @code{volatile} and @code{restrict}
1209*c42dbd0eSchristosqualifiers are represented identically except for their type kind (though they
1210*c42dbd0eSchristosmay be treated differently by consuming libraries like @code{libctf}, since
1211*c42dbd0eSchristospointers affect assignment-compatibility in ways cvr-quals do not, and they may
1212*c42dbd0eSchristoshave different alignment requirements, etc).
1213*c42dbd0eSchristos
1214*c42dbd0eSchristosAll of these are represented by @code{ctf_stype_t}, have no variable data at
1215*c42dbd0eSchristosall, and populate @code{ctt_type} with the type ID of the type they point
1216*c42dbd0eSchristosto. These types can stack: a @code{CTF_K_RESTRICT} can point to a
1217*c42dbd0eSchristos@code{CTF_K_CONST} which can point to a @code{CTF_K_POINTER} etc.
1218*c42dbd0eSchristos
1219*c42dbd0eSchristosThey are all unnamed: @code{ctt_name} is 0.
1220*c42dbd0eSchristos
1221*c42dbd0eSchristosThe size of @code{CTF_K_POINTER} is derived from the data model (@pxref{Data
1222*c42dbd0eSchristosmodels}), i.e. in practice, from the target machine ABI, and is not explicitly
1223*c42dbd0eSchristosrepresented.  The size of other kinds in this set should be determined by
1224*c42dbd0eSchristoschasing ctf_types as necessary until a non-typedef/const/volatile/restrict is
1225*c42dbd0eSchristosfound, and using that.
1226*c42dbd0eSchristos
1227*c42dbd0eSchristos@node Arrays
1228*c42dbd0eSchristos@subsection Arrays
1229*c42dbd0eSchristos@cindex Arrays
1230*c42dbd0eSchristos
1231*c42dbd0eSchristosArrays are encoded as types of kind @code{CTF_K_ARRAY} in a @code{ctf_stype_t}.
1232*c42dbd0eSchristosBoth size and kind for arrays are zero.  The variable-length data is a
1233*c42dbd0eSchristos@code{ctf_array_t}: @code{vlen} in the info word should be disregarded and is
1234*c42dbd0eSchristosalways zero.
1235*c42dbd0eSchristos
1236*c42dbd0eSchristos@verbatim
1237*c42dbd0eSchristostypedef struct ctf_array
1238*c42dbd0eSchristos{
1239*c42dbd0eSchristos  uint32_t cta_contents;
1240*c42dbd0eSchristos  uint32_t cta_index;
1241*c42dbd0eSchristos  uint32_t cta_nelems;
1242*c42dbd0eSchristos} ctf_array_t;
1243*c42dbd0eSchristos@end verbatim
1244*c42dbd0eSchristos
1245*c42dbd0eSchristos@tindex struct ctf_array
1246*c42dbd0eSchristos@tindex ctf_array_t
1247*c42dbd0eSchristos@multitable {Offset} {@code{unsigned short cta_contents}} {The type of the array index: a type ID of an}
1248*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1249*c42dbd0eSchristos@item 0x0
1250*c42dbd0eSchristos@tab @code{uint32_t cta_contents}
1251*c42dbd0eSchristos@vindex cta_contents
1252*c42dbd0eSchristos@vindex struct ctf_array, cta_contents
1253*c42dbd0eSchristos@vindex ctf_array_t, cta_contents
1254*c42dbd0eSchristos@tab The type of the array elements: a type ID.
1255*c42dbd0eSchristos
1256*c42dbd0eSchristos@item 0x4
1257*c42dbd0eSchristos@tab @code{uint32_t cta_index}
1258*c42dbd0eSchristos@vindex cta_index
1259*c42dbd0eSchristos@vindex struct ctf_array, cta_index
1260*c42dbd0eSchristos@vindex ctf_array_t, cta_index
1261*c42dbd0eSchristos@tab The type of the array index: a type ID of an integral type.
1262*c42dbd0eSchristosIf this is a variable-length array, the index type ID will be 0
1263*c42dbd0eSchristos(but the actual index type of this array is probably @code{int}).
1264*c42dbd0eSchristosProbably redundant and may be dropped in v4.
1265*c42dbd0eSchristos
1266*c42dbd0eSchristos@item 0x8
1267*c42dbd0eSchristos@tab @code{uint32_t cta_nelems}
1268*c42dbd0eSchristos@vindex cta_nelems
1269*c42dbd0eSchristos@vindex struct ctf_array, cta_nelems
1270*c42dbd0eSchristos@vindex ctf_array_t, cta_nelems
1271*c42dbd0eSchristos@tab The number of array elements.  0 for VLAs, and also for
1272*c42dbd0eSchristosthe historical variety of VLA which has explicit zero dimensions (which will
1273*c42dbd0eSchristoshave a nonzero @code{cta_index}.)
1274*c42dbd0eSchristos@end multitable
1275*c42dbd0eSchristos
1276*c42dbd0eSchristosThe size of an array can be computed by simple multiplication of the size of the
1277*c42dbd0eSchristos@code{cta_contents} type by the @code{cta_nelems}.
1278*c42dbd0eSchristos
1279*c42dbd0eSchristos@node Function pointers
1280*c42dbd0eSchristos@subsection Function pointers
1281*c42dbd0eSchristos@cindex Function pointers
1282*c42dbd0eSchristos@cindex Pointers, to functions
1283*c42dbd0eSchristos
1284*c42dbd0eSchristosFunction pointers are explicitly represented in the CTF type section by a type
1285*c42dbd0eSchristosof kind @code{CTF_K_FUNCTION}, always encoded with a @code{ctf_stype_t}.  The
1286*c42dbd0eSchristos@code{ctt_type} is the function return type ID.  The @code{vlen} in the info
1287*c42dbd0eSchristosword is the number of arguments, each of which is a type ID, a @code{uint32_t}:
1288*c42dbd0eSchristosif the last argument is 0, this is a varargs function and the number of
1289*c42dbd0eSchristosarguments is one less than indicated by the vlen.
1290*c42dbd0eSchristos
1291*c42dbd0eSchristosIf the number of arguments is odd, a single @code{uint32_t} of padding is
1292*c42dbd0eSchristosinserted to maintain alignment.
1293*c42dbd0eSchristos
1294*c42dbd0eSchristos@node Enums
1295*c42dbd0eSchristos@subsection Enums
1296*c42dbd0eSchristos@cindex Enums
1297*c42dbd0eSchristos@tindex enum
1298*c42dbd0eSchristos@tindex CTF_K_ENUM
1299*c42dbd0eSchristos
1300*c42dbd0eSchristosEnumerated types are represented as types of kind @code{CTF_K_ENUM} in a
1301*c42dbd0eSchristos@code{ctf_stype_t}.  The @code{ctt_size} is always the size of an int from the
1302*c42dbd0eSchristosdata model (enum bitfields are implemented via slices).  The @code{vlen} is a
1303*c42dbd0eSchristoscount of enumerations, each of which is represented by a @code{ctf_enum_t} in
1304*c42dbd0eSchristosthe vlen:
1305*c42dbd0eSchristos
1306*c42dbd0eSchristos@verbatim
1307*c42dbd0eSchristostypedef struct ctf_enum
1308*c42dbd0eSchristos{
1309*c42dbd0eSchristos  uint32_t cte_name;
1310*c42dbd0eSchristos  int32_t cte_value;
1311*c42dbd0eSchristos} ctf_enum_t;
1312*c42dbd0eSchristos@end verbatim
1313*c42dbd0eSchristos
1314*c42dbd0eSchristos@tindex struct ctf_enum
1315*c42dbd0eSchristos@tindex ctf_enum_t
1316*c42dbd0eSchristos@multitable {Offset} {@code{int32_t cte_value}} {Strtab offset of the enumeration name.}
1317*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1318*c42dbd0eSchristos@item 0x0
1319*c42dbd0eSchristos@tab @code{uint32_t cte_name}
1320*c42dbd0eSchristos@vindex cte_name
1321*c42dbd0eSchristos@vindex struct ctf_enum, cte_name
1322*c42dbd0eSchristos@vindex ctf_enum_t, cte_name
1323*c42dbd0eSchristos@tab Strtab offset of the enumeration name.  Must not be 0.
1324*c42dbd0eSchristos
1325*c42dbd0eSchristos@item 0x4
1326*c42dbd0eSchristos@tab @code{int32_t cte_value}
1327*c42dbd0eSchristos@vindex cte_value
1328*c42dbd0eSchristos@vindex struct ctf_enum, cte_value
1329*c42dbd0eSchristos@vindex ctf_enum_t, cte_value
1330*c42dbd0eSchristos@tab The enumeration value.
1331*c42dbd0eSchristos
1332*c42dbd0eSchristos@end multitable
1333*c42dbd0eSchristos
1334*c42dbd0eSchristosEnumeration values larger than @math{2^32} are not yet supported and are omitted
1335*c42dbd0eSchristosfrom the enumeration.  (v4 will lift this restriction by encoding the value
1336*c42dbd0eSchristosdifferently.)
1337*c42dbd0eSchristos
1338*c42dbd0eSchristosForward declarations of enums are not implemented with this kind: @pxref{Forward
1339*c42dbd0eSchristosdeclarations}.
1340*c42dbd0eSchristos
1341*c42dbd0eSchristosEnumerated type names, as usual in C, go into their own namespace, and do not
1342*c42dbd0eSchristosconflict with non-enums, structs, or unions with the same name.
1343*c42dbd0eSchristos
1344*c42dbd0eSchristos@node Structs and unions
1345*c42dbd0eSchristos@subsection Structs and unions
1346*c42dbd0eSchristos@cindex Structures
1347*c42dbd0eSchristos@cindex Unions
1348*c42dbd0eSchristos@tindex struct
1349*c42dbd0eSchristos@tindex union
1350*c42dbd0eSchristos@tindex CTF_K_STRUCT
1351*c42dbd0eSchristos@tindex CTF_K_UNION
1352*c42dbd0eSchristos
1353*c42dbd0eSchristosStructures and unions are represnted as types of kind @code{CTF_K_STRUCT} and
1354*c42dbd0eSchristos@code{CTF_K_UNION}: their representation is otherwise identical, and it is
1355*c42dbd0eSchristosperfectly allowed for ``structs'' to contain overlapping fields etc, so we will
1356*c42dbd0eSchristostreat them together for the rest of this section.
1357*c42dbd0eSchristos
1358*c42dbd0eSchristosThey fill out @code{ctt_size}, and use @code{ctf_type_t} in preference to
1359*c42dbd0eSchristos@code{ctf_stype_t} if the structure size is greater than @code{CTF_MAX_SIZE}
1360*c42dbd0eSchristos(0xfffffffe).
1361*c42dbd0eSchristos@tindex CTF_MAX_LSIZE
1362*c42dbd0eSchristos
1363*c42dbd0eSchristosThe vlen for structures and unions is a count of structure fields, but the type
1364*c42dbd0eSchristosused to represent a structure field (and thus the size of the variable-length
1365*c42dbd0eSchristosarray element representing the type) depends on the size of the structure: truly
1366*c42dbd0eSchristoshuge structures, greater than @code{CTF_LSTRUCT_THRESH} bytes in length, use a
1367*c42dbd0eSchristosdifferent type.  (@code{CTF_LSTRUCT_THRESH} is 536870912, so such structures are
1368*c42dbd0eSchristosvanishingly rare: in v4, this representation will change somewhat for greater
1369*c42dbd0eSchristoscompactness. It's inherited from v1, where the limits were much lower.)
1370*c42dbd0eSchristos@tindex CTF_LSTRUCT_THRESH
1371*c42dbd0eSchristos
1372*c42dbd0eSchristosMost structures can get away with using @code{ctf_member_t}:
1373*c42dbd0eSchristos
1374*c42dbd0eSchristos@verbatim
1375*c42dbd0eSchristostypedef struct ctf_member_v2
1376*c42dbd0eSchristos{
1377*c42dbd0eSchristos  uint32_t ctm_name;
1378*c42dbd0eSchristos  uint32_t ctm_offset;
1379*c42dbd0eSchristos  uint32_t ctm_type;
1380*c42dbd0eSchristos} ctf_member_t;
1381*c42dbd0eSchristos@end verbatim
1382*c42dbd0eSchristos
1383*c42dbd0eSchristosHuge structures that are represented by @code{ctf_type_t} rather than
1384*c42dbd0eSchristos@code{ctf_stype_t} have to use @code{ctf_lmember_t}, which splits the offset as
1385*c42dbd0eSchristos@code{ctf_type_t} splits the size:
1386*c42dbd0eSchristos
1387*c42dbd0eSchristos@verbatim
1388*c42dbd0eSchristostypedef struct ctf_lmember_v2
1389*c42dbd0eSchristos{
1390*c42dbd0eSchristos  uint32_t ctlm_name;
1391*c42dbd0eSchristos  uint32_t ctlm_offsethi;
1392*c42dbd0eSchristos  uint32_t ctlm_type;
1393*c42dbd0eSchristos  uint32_t ctlm_offsetlo;
1394*c42dbd0eSchristos} ctf_lmember_t;
1395*c42dbd0eSchristos@end verbatim
1396*c42dbd0eSchristos
1397*c42dbd0eSchristosHere's what the fields of @code{ctf_member} mean:
1398*c42dbd0eSchristos
1399*c42dbd0eSchristos@tindex struct ctf_member_v2
1400*c42dbd0eSchristos@tindex ctf_member_t
1401*c42dbd0eSchristos@multitable {Offset} {@code{uint32_t ctm_offset}} {The offset of this field @emph{in bits}.  (Usually, for bitfields, this is}
1402*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1403*c42dbd0eSchristos@item 0x00
1404*c42dbd0eSchristos@tab @code{uint32_t ctm_name}
1405*c42dbd0eSchristos@vindex ctm_name
1406*c42dbd0eSchristos@vindex struct ctf_member_v2, ctm_name
1407*c42dbd0eSchristos@vindex ctf_member_t, ctm_name
1408*c42dbd0eSchristos@tab Strtab offset of the field name.
1409*c42dbd0eSchristos
1410*c42dbd0eSchristos@item 0x04
1411*c42dbd0eSchristos@tab @code{uint32_t ctm_offset}
1412*c42dbd0eSchristos@vindex ctm_offset
1413*c42dbd0eSchristos@vindex struct ctf_member_v2, ctm_offset
1414*c42dbd0eSchristos@vindex ctf_member_t, ctm_offset
1415*c42dbd0eSchristos@tab The offset of this field @emph{in bits}.  (Usually, for bitfields, this is
1416*c42dbd0eSchristosmachine-word-aligned and the individual field has an offset in bits, but
1417*c42dbd0eSchristosthe format allows for the offset to be encoded in bits here.)
1418*c42dbd0eSchristos
1419*c42dbd0eSchristos@item 0x08
1420*c42dbd0eSchristos@tab @code{uint32_t ctm_type}
1421*c42dbd0eSchristos@vindex ctm_type
1422*c42dbd0eSchristos@vindex struct ctf_member_v2, ctm_type
1423*c42dbd0eSchristos@vindex ctf_member_t, ctm_type
1424*c42dbd0eSchristos@tab The type ID of the type of the field.
1425*c42dbd0eSchristos@end multitable
1426*c42dbd0eSchristos
1427*c42dbd0eSchristosHere's what the fields of the very similar @code{ctf_lmember} mean:
1428*c42dbd0eSchristos
1429*c42dbd0eSchristos@tindex struct ctf_lmember_v2
1430*c42dbd0eSchristos@tindex ctf_lmember_t
1431*c42dbd0eSchristos@multitable {Offset} {@code{uint32_t ctlm_offsethi}} {The offset of this field @emph{in bits}.  (Usually, for bitfields, this is}
1432*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1433*c42dbd0eSchristos@item 0x00
1434*c42dbd0eSchristos@tab @code{uint32_t ctlm_name}
1435*c42dbd0eSchristos@vindex ctlm_name
1436*c42dbd0eSchristos@vindex struct ctf_lmember_v2, ctlm_name
1437*c42dbd0eSchristos@vindex ctf_lmember_t, ctlm_name
1438*c42dbd0eSchristos@tab Strtab offset of the field name.
1439*c42dbd0eSchristos
1440*c42dbd0eSchristos@item 0x04
1441*c42dbd0eSchristos@tab @code{uint32_t ctlm_offsethi}
1442*c42dbd0eSchristos@vindex ctlm_offsethi
1443*c42dbd0eSchristos@vindex struct ctf_lmember_v2, ctlm_offsethi
1444*c42dbd0eSchristos@vindex ctf_lmember_t, ctlm_offsethi
1445*c42dbd0eSchristos@tab The high 32 bits of the offset of this field in bits.
1446*c42dbd0eSchristos
1447*c42dbd0eSchristos@item 0x08
1448*c42dbd0eSchristos@tab @code{uint32_t ctlm_type}
1449*c42dbd0eSchristos@vindex ctm_type
1450*c42dbd0eSchristos@vindex struct ctf_lmember_v2, ctlm_type
1451*c42dbd0eSchristos@vindex ctf_member_t, ctlm_type
1452*c42dbd0eSchristos@tab The type ID of the type of the field.
1453*c42dbd0eSchristos
1454*c42dbd0eSchristos@item 0x0c
1455*c42dbd0eSchristos@tab @code{uint32_t ctlm_offsetlo}
1456*c42dbd0eSchristos@vindex ctlm_offsetlo
1457*c42dbd0eSchristos@vindex struct ctf_lmember_v2, ctlm_offsetlo
1458*c42dbd0eSchristos@vindex ctf_lmember_t, ctlm_offsetlo
1459*c42dbd0eSchristos@tab The low 32 bits of the offset of this field in bits.
1460*c42dbd0eSchristos@end multitable
1461*c42dbd0eSchristos
1462*c42dbd0eSchristosMacros @code{CTF_LMEM_OFFSET}, @code{CTF_OFFSET_TO_LMEMHI} and
1463*c42dbd0eSchristos@code{CTF_OFFSET_TO_LMEMLO} serve to extract and install the values of the
1464*c42dbd0eSchristos@code{ctlm_offset} fields, much as with the split size fields in
1465*c42dbd0eSchristos@code{ctf_type_t}.
1466*c42dbd0eSchristos
1467*c42dbd0eSchristosUnnamed structure and union fields are simply implemented by collapsing the
1468*c42dbd0eSchristosunnamed field's members into the containing structure or union: this does mean
1469*c42dbd0eSchristosthat a structure containing an unnamed union can end up being a ``structure''
1470*c42dbd0eSchristoswith multiple members at the same offset.  (A future format revision may
1471*c42dbd0eSchristoscollapse @code{CTF_K_STRUCT} and @code{CTF_K_UNION} into the same kind and
1472*c42dbd0eSchristosdecide among them based on whether their members do in fact overlap.)
1473*c42dbd0eSchristos
1474*c42dbd0eSchristosStructure and union type names, as usual in C, go into their own namespace,
1475*c42dbd0eSchristosjust as enum type names do.
1476*c42dbd0eSchristos
1477*c42dbd0eSchristosForward declarations of structures and unions are not implemented with this
1478*c42dbd0eSchristoskind: @pxref{Forward declarations}.
1479*c42dbd0eSchristos
1480*c42dbd0eSchristos@node Forward declarations
1481*c42dbd0eSchristos@subsection Forward declarations
1482*c42dbd0eSchristos@cindex Forwards
1483*c42dbd0eSchristos@tindex enum
1484*c42dbd0eSchristos@tindex struct
1485*c42dbd0eSchristos@tindex union
1486*c42dbd0eSchristos@tindex CTF_K_FORWARD
1487*c42dbd0eSchristos
1488*c42dbd0eSchristosWhen the compiler encounters a forward declaration of a struct, union, or enum,
1489*c42dbd0eSchristosit emits a type of kind @code{CTF_K_FORWARD}.  If it later encounters a non-
1490*c42dbd0eSchristosforward declaration of the same thing, it marks the forward as non-root-visible:
1491*c42dbd0eSchristosbefore link time, therefore, non-root-visible forwards indicate that a
1492*c42dbd0eSchristosnon-forward is coming.
1493*c42dbd0eSchristos
1494*c42dbd0eSchristosAfter link time, forwards are fused with their corresponding non-forwards by the
1495*c42dbd0eSchristosdeduplicator where possible.  They are kept if there is no non-forward
1496*c42dbd0eSchristosdefinition (maybe it's not visible from any TU at all) or if @code{multiple}
1497*c42dbd0eSchristosconflicting structures with the same name might match it.  Otherwise, all other
1498*c42dbd0eSchristosforwards are converted to structures, unions, or enums as appropriate, even
1499*c42dbd0eSchristosacross TUs if only one structure could correspond to the forward (after all,
1500*c42dbd0eSchristosall types across all TUs land in the same dictionary unless they conflict,
1501*c42dbd0eSchristosso promoting forwards to their concrete type seems most helpful).
1502*c42dbd0eSchristos
1503*c42dbd0eSchristosA forward has a rather strange representation: it is encoded with a
1504*c42dbd0eSchristos@code{ctf_stype_t} but the @code{ctt_type} is populated not with a type (if it's
1505*c42dbd0eSchristosa forward, we don't have an underlying type yet: if we did, we'd have promoted
1506*c42dbd0eSchristosit and this wouldn't be a forward any more) but with the @code{kind} of the
1507*c42dbd0eSchristosforward.  This means that we can distinguish forwards to structs, enums and
1508*c42dbd0eSchristosunions reliably and ensure they land in the appropriate namespace even before
1509*c42dbd0eSchristosthe actual struct, union or enum is found.
1510*c42dbd0eSchristos
1511*c42dbd0eSchristos@node The symtypetab sections
1512*c42dbd0eSchristos@section The symtypetab sections
1513*c42dbd0eSchristos@cindex Symtypetab section
1514*c42dbd0eSchristos@cindex Sections, symtypetab
1515*c42dbd0eSchristos@cindex Function info section
1516*c42dbd0eSchristos@cindex Sections, function info
1517*c42dbd0eSchristos@cindex Data object section
1518*c42dbd0eSchristos@cindex Sections, data object
1519*c42dbd0eSchristos@cindex Function info index section
1520*c42dbd0eSchristos@cindex Sections, function info index
1521*c42dbd0eSchristos@cindex Data object index section
1522*c42dbd0eSchristos@cindex Sections, data object index
1523*c42dbd0eSchristos@tindex CTF_F_IDXSORTED
1524*c42dbd0eSchristos@tindex CTF_F_DYNSTR
1525*c42dbd0eSchristos@cindex Bug workarounds, CTF_F_DYNSTR
1526*c42dbd0eSchristos
1527*c42dbd0eSchristosThese are two very simple sections with identical formats, used by consumers to
1528*c42dbd0eSchristosmap from ELF function and data symbols directly to their types. So they are
1529*c42dbd0eSchristosusually populated only in CTF sections that are embedded in ELF objects.
1530*c42dbd0eSchristos
1531*c42dbd0eSchristosTheir format is very simple: an array of type IDs. Which symbol each type ID
1532*c42dbd0eSchristoscorresponds to depends on whether the optional @emph{index section} associated
1533*c42dbd0eSchristoswith this symtypetab section has any content.
1534*c42dbd0eSchristos
1535*c42dbd0eSchristosIf the index section is nonempty, it is an array of @code{uint32_t} string table
1536*c42dbd0eSchristosoffsets, each giving the name of the symbol whose type is at the same offset in
1537*c42dbd0eSchristosthe corresponding non-index section: users can look up symbols in such a table
1538*c42dbd0eSchristosby name.  The index section and corresponding symtypetab section is usually
1539*c42dbd0eSchristosASCIIbetically sorted (indicated by the @code{CTF_F_IDXSORTED} flag in the
1540*c42dbd0eSchristosheader): if it's sorted, it can be bsearched for a symbol name rather than
1541*c42dbd0eSchristoshaving to use a slower linear search.
1542*c42dbd0eSchristos
1543*c42dbd0eSchristosIf the data object index section is empty, the entries in the data object and
1544*c42dbd0eSchristosfunction info sections are associated 1:1 with ELF symbols of type
1545*c42dbd0eSchristos@code{STT_OBJECT} (for data object) or @code{STT_FUNC} (for function info) with
1546*c42dbd0eSchristosa nonzero value: the linker shuffles the symtypetab sections to correspond with
1547*c42dbd0eSchristosthe order of the symbols in the ELF file.  Symbols with no name, undefined
1548*c42dbd0eSchristossymbols and symbols named ``@code{_START_}'' and ``@code{_END_}'' are skipped
1549*c42dbd0eSchristosand never appear in either section.  Symbols that have no corresponding type are
1550*c42dbd0eSchristosrepresented by type ID 0. The section may have fewer entries than the symbol
1551*c42dbd0eSchristostable, in which case no later entries have associated types.  This format is
1552*c42dbd0eSchristosmore compact than an indexed form if most entries have types (since there is no
1553*c42dbd0eSchristosneed to record any symbol names), but if the producer and consumer disagree even
1554*c42dbd0eSchristosslightly about which symbols are omitted, the types of all further symbols will
1555*c42dbd0eSchristosbe wrong!
1556*c42dbd0eSchristos
1557*c42dbd0eSchristosThe compiler always emits indexed symtypetab tables, because there is no symbol
1558*c42dbd0eSchristostable yet. The linker will always have to read them all in and always works
1559*c42dbd0eSchristosthrough them from start to end, so there is no benefit having the compiler sort
1560*c42dbd0eSchristosthem either. The linker (actually, @code{libctf}'s linking machinery) will
1561*c42dbd0eSchristosautomatically sort unsorted indexed sections, and convert indexed sections that
1562*c42dbd0eSchristoscontain a lot of pads into the more compact, unindexed form.
1563*c42dbd0eSchristos
1564*c42dbd0eSchristosIf child dicts are in use, only symbols that use types actually mentioned in the
1565*c42dbd0eSchristoschild appear in the child's symtypetab: symbols that use only types in the
1566*c42dbd0eSchristosparent appear in the parent's symtypetab instead. So the child's symtypetab will
1567*c42dbd0eSchristosalmost always be very sparse, and thus will usually use the indexed form even in
1568*c42dbd0eSchristosfully linked objects. (It is, of course, impossible for symbols to exist that
1569*c42dbd0eSchristosuse types from multiple child dicts at once, since it's impossible to declare a
1570*c42dbd0eSchristosfunction in C that uses types that are only visible in two different, disjoint
1571*c42dbd0eSchristostranslation units.)
1572*c42dbd0eSchristos
1573*c42dbd0eSchristos@node The variable section
1574*c42dbd0eSchristos@section The variable section
1575*c42dbd0eSchristos@cindex Variable section
1576*c42dbd0eSchristos@cindex Sections, variable
1577*c42dbd0eSchristos
1578*c42dbd0eSchristosThe variable section is a simple array mapping names (strtab entries) to type
1579*c42dbd0eSchristosIDs, intended to provide a replacement for the data object section in dynamic
1580*c42dbd0eSchristossituations in which there is no static ELF strtab but the consumer instead hands
1581*c42dbd0eSchristosback names.  The section is sorted into ASCIIbetical order by name for rapid
1582*c42dbd0eSchristoslookup, like the CTF archive name table.
1583*c42dbd0eSchristos
1584*c42dbd0eSchristosThe section is an array of these structures:
1585*c42dbd0eSchristos
1586*c42dbd0eSchristos@verbatim
1587*c42dbd0eSchristostypedef struct ctf_varent
1588*c42dbd0eSchristos{
1589*c42dbd0eSchristos  uint32_t ctv_name;
1590*c42dbd0eSchristos  uint32_t ctv_type;
1591*c42dbd0eSchristos} ctf_varent_t;
1592*c42dbd0eSchristos@end verbatim
1593*c42dbd0eSchristos
1594*c42dbd0eSchristos@tindex struct ctf_varent
1595*c42dbd0eSchristos@tindex ctf_varent_t
1596*c42dbd0eSchristos@multitable {Offset} {@code{uint32_t ctv_name}} {Strtab offset of the name}
1597*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1598*c42dbd0eSchristos@item 0x00
1599*c42dbd0eSchristos@tab @code{uint32_t ctv_name}
1600*c42dbd0eSchristos@vindex ctv_name
1601*c42dbd0eSchristos@vindex struct ctf_varent, ctv_name
1602*c42dbd0eSchristos@vindex ctf_varent_t, ctv_name
1603*c42dbd0eSchristos@tab Strtab offset of the name
1604*c42dbd0eSchristos
1605*c42dbd0eSchristos@item 0x04
1606*c42dbd0eSchristos@tab @code{uint32_t ctv_type}
1607*c42dbd0eSchristos@vindex ctv_type
1608*c42dbd0eSchristos@vindex struct ctf_varent, ctv_type
1609*c42dbd0eSchristos@vindex ctf_varent_t, ctv_type
1610*c42dbd0eSchristos@tab Type ID of this type
1611*c42dbd0eSchristos@end multitable
1612*c42dbd0eSchristos
1613*c42dbd0eSchristosThere is no analogue of the function info section yet: v4 will probably drop
1614*c42dbd0eSchristosthis section in favour of a way to put both indexed (thus, named) and nonindexed
1615*c42dbd0eSchristossymbols into the symtypetab sections at the same time.
1616*c42dbd0eSchristos
1617*c42dbd0eSchristos@node The label section
1618*c42dbd0eSchristos@section The label section
1619*c42dbd0eSchristos@cindex Label section
1620*c42dbd0eSchristos@cindex Sections, label
1621*c42dbd0eSchristos
1622*c42dbd0eSchristosThe label section is a currently-unused facility allowing the tiling of the type
1623*c42dbd0eSchristosspace with names taken from the strtab.  The section is an array of these
1624*c42dbd0eSchristosstructures:
1625*c42dbd0eSchristos
1626*c42dbd0eSchristos@verbatim
1627*c42dbd0eSchristostypedef struct ctf_lblent
1628*c42dbd0eSchristos{
1629*c42dbd0eSchristos  uint32_t ctl_label;
1630*c42dbd0eSchristos  uint32_t ctl_type;
1631*c42dbd0eSchristos} ctf_lblent_t;
1632*c42dbd0eSchristos@end verbatim
1633*c42dbd0eSchristos
1634*c42dbd0eSchristos@tindex struct ctf_lblent
1635*c42dbd0eSchristos@tindex ctf_lblent_t
1636*c42dbd0eSchristos@multitable {Offset} {@code{uint32_t ctl_label}} {Strtab offset of the label}
1637*c42dbd0eSchristos@headitem Offset @tab Name @tab Description
1638*c42dbd0eSchristos@item 0x00
1639*c42dbd0eSchristos@tab @code{uint32_t ctl_label}
1640*c42dbd0eSchristos@vindex ctl_label
1641*c42dbd0eSchristos@vindex struct ctf_lblent, ctl_label
1642*c42dbd0eSchristos@vindex ctf_lblent_t, ctl_label
1643*c42dbd0eSchristos@tab Strtab offset of the label
1644*c42dbd0eSchristos
1645*c42dbd0eSchristos@item 0x04
1646*c42dbd0eSchristos@tab @code{uint32_t ctl_type}
1647*c42dbd0eSchristos@vindex ctl_type
1648*c42dbd0eSchristos@vindex struct ctf_lblent, ctl_type
1649*c42dbd0eSchristos@vindex ctf_lblent_t, ctl_type
1650*c42dbd0eSchristos@tab Type ID of the last type covered by this label
1651*c42dbd0eSchristos@end multitable
1652*c42dbd0eSchristos
1653*c42dbd0eSchristosSemantics will be attached to labels soon, probably in v4 (the plan is to use
1654*c42dbd0eSchristosthem to allow multiple disjoint namespaces in a single CTF file, removing many
1655*c42dbd0eSchristosuses of CTF archives, in particular in the @code{.ctf} section in ELF objects).
1656*c42dbd0eSchristos
1657*c42dbd0eSchristos@node The string section
1658*c42dbd0eSchristos@section The string section
1659*c42dbd0eSchristos@cindex String section
1660*c42dbd0eSchristos@cindex Sections, string
1661*c42dbd0eSchristos
1662*c42dbd0eSchristosThis section is a simple ELF-format strtab, starting with a zero byte (thus
1663*c42dbd0eSchristosensuring that the string with offset 0 is the null string, as assumed elsewhere
1664*c42dbd0eSchristosin this spec).  The strtab is usually ASCIIbetically sorted to somewhat improve
1665*c42dbd0eSchristoscompression efficiency.
1666*c42dbd0eSchristos
1667*c42dbd0eSchristosWhere the strtab is unusual is the @emph{references} to it.  CTF has two
1668*c42dbd0eSchristosstring tables, the internal strtab and an external strtab associated
1669*c42dbd0eSchristoswith the CTF dictionary at open time: usually, this is the ELF dynamic
1670*c42dbd0eSchristosstrtab (@code{.dynstr}) of a CTF dictionary embedded in an ELF file.  We
1671*c42dbd0eSchristosdistinguish between these strtabs by the most significant bit, bit 31,
1672*c42dbd0eSchristosof the 32-bit strtab references: if it is 0, the offset is in the
1673*c42dbd0eSchristosinternal strtab: if 1, the offset is in the external strtab.
1674*c42dbd0eSchristos
1675*c42dbd0eSchristos@tindex CTF_F_DYNSTR
1676*c42dbd0eSchristos@cindex Bug workarounds, CTF_F_DYNSTR
1677*c42dbd0eSchristosThere is a bug workaround in this area: in format v3 (the first version
1678*c42dbd0eSchristosto have working support for external strtabs), the external strtab is
1679*c42dbd0eSchristos@code{.strtab} unless the @code{CTF_F_DYNSTR} flag is set on the
1680*c42dbd0eSchristosdictionary (@pxref{CTF file-wide flags}).  Format v4 will introduce a
1681*c42dbd0eSchristosheader field that explicitly names the external strtab, making this flag
1682*c42dbd0eSchristosunnecessary.
1683*c42dbd0eSchristos
1684*c42dbd0eSchristos@node Data models
1685*c42dbd0eSchristos@section Data models
1686*c42dbd0eSchristos@cindex Data models
1687*c42dbd0eSchristos
1688*c42dbd0eSchristosThe data model is a simple integer which indicates the ABI in use on this
1689*c42dbd0eSchristosplatform. Right now, it is very simple, distinguishing only between 32- and
1690*c42dbd0eSchristos64-bit types: a model of 1 indicates ILP32, 2 indicats LP64. The mapping from
1691*c42dbd0eSchristosABI integer to type sizes is hardwired into @code{libctf}: currently, we use
1692*c42dbd0eSchristosthis to hardwire the size of pointers, function pointers, and enumerated types,
1693*c42dbd0eSchristos
1694*c42dbd0eSchristosThis is a very kludgy corner of CTF and will probably be replaced with explicit
1695*c42dbd0eSchristosheader fields to record this sort of thing in future.
1696*c42dbd0eSchristos
1697*c42dbd0eSchristos@node Limits of CTF
1698*c42dbd0eSchristos@section Limits of CTF
1699*c42dbd0eSchristos@cindex Limits
1700*c42dbd0eSchristos
1701*c42dbd0eSchristosThe following limits are imposed by various aspects of CTF version 3:
1702*c42dbd0eSchristos
1703*c42dbd0eSchristos@table @code
1704*c42dbd0eSchristos@item CTF_MAX_TYPE
1705*c42dbd0eSchristosMaximum type identifier (maximum number of types accessible with parent and
1706*c42dbd0eSchristoschild containers in use): 0xfffffffe
1707*c42dbd0eSchristos@item CTF_MAX_PTYPE
1708*c42dbd0eSchristosMaximum type identifier in a parent dictioanry: maximum number of types in any
1709*c42dbd0eSchristosone dictionary: 0x7fffffff
1710*c42dbd0eSchristos@item CTF_MAX_NAME
1711*c42dbd0eSchristosMaximum offset into a string table: 0x7fffffff
1712*c42dbd0eSchristos@item CTF_MAX_VLEN
1713*c42dbd0eSchristosMaximum number of members in a struct, union, or enum: maximum number of
1714*c42dbd0eSchristosfunction args: 0xffffff
1715*c42dbd0eSchristos@item CTF_MAX_SIZE
1716*c42dbd0eSchristosMaximum size of a @code{ctf_stype_t} in bytes before we fall back to
1717*c42dbd0eSchristos@code{ctf_type_t}: 0xfffffffe bytes
1718*c42dbd0eSchristos@end table
1719*c42dbd0eSchristos
1720*c42dbd0eSchristosOther maxima without associated macros:
1721*c42dbd0eSchristos@itemize
1722*c42dbd0eSchristos@item
1723*c42dbd0eSchristosMaximum value of an enumerated type: 2^32
1724*c42dbd0eSchristos@item
1725*c42dbd0eSchristosMaximum size of an array element: 2^32
1726*c42dbd0eSchristos@end itemize
1727*c42dbd0eSchristos
1728*c42dbd0eSchristosThese maxima are generally considered to be too low, because C programs can and
1729*c42dbd0eSchristosdo exceed them: they will be lifted in format v4.
1730*c42dbd0eSchristos
1731*c42dbd0eSchristos@node Index
1732*c42dbd0eSchristos@unnumbered Index
1733*c42dbd0eSchristos
1734*c42dbd0eSchristos@printindex cp
1735*c42dbd0eSchristos
1736*c42dbd0eSchristos@bye
1737