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