xref: /titanic_41/usr/src/man/man4/ctf.4 (revision 894a1ed8a0c966aed5551095a115b69f725e5779)
1*894a1ed8SRobert Mustacchi.\"
2*894a1ed8SRobert Mustacchi.\" This file and its contents are supplied under the terms of the
3*894a1ed8SRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0.
4*894a1ed8SRobert Mustacchi.\" You may only use this file in accordance with the terms of version
5*894a1ed8SRobert Mustacchi.\" 1.0 of the CDDL.
6*894a1ed8SRobert Mustacchi.\"
7*894a1ed8SRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this
8*894a1ed8SRobert Mustacchi.\" source.  A copy of the CDDL is also available via the Internet at
9*894a1ed8SRobert Mustacchi.\" http://www.illumos.org/license/CDDL.
10*894a1ed8SRobert Mustacchi.\"
11*894a1ed8SRobert Mustacchi.\"
12*894a1ed8SRobert Mustacchi.\" Copyright (c) 2014 Joyent, Inc.
13*894a1ed8SRobert Mustacchi.\"
14*894a1ed8SRobert Mustacchi.Dd Sep 26, 2014
15*894a1ed8SRobert Mustacchi.Dt CTF 4
16*894a1ed8SRobert Mustacchi.Os
17*894a1ed8SRobert Mustacchi.Sh NAME
18*894a1ed8SRobert Mustacchi.Nm ctf
19*894a1ed8SRobert Mustacchi.Nd Compact C Type Format
20*894a1ed8SRobert Mustacchi.Sh SYNOPSIS
21*894a1ed8SRobert Mustacchi.In sys/ctf.h
22*894a1ed8SRobert Mustacchi.Sh DESCRIPTION
23*894a1ed8SRobert Mustacchi.Nm
24*894a1ed8SRobert Mustacchiis designed to be a compact representation of the C programming
25*894a1ed8SRobert Mustacchilanguage's type information focused on serving the needs of dynamic
26*894a1ed8SRobert Mustacchitracing, debuggers, and other in-situ and post-mortem introspection
27*894a1ed8SRobert Mustacchitools.
28*894a1ed8SRobert Mustacchi.Nm
29*894a1ed8SRobert Mustacchidata is generally included in
30*894a1ed8SRobert Mustacchi.Sy ELF
31*894a1ed8SRobert Mustacchiobjects and is tagged as
32*894a1ed8SRobert Mustacchi.Sy SHT_PROGBITS
33*894a1ed8SRobert Mustacchito ensure that the data is accessible in a running process and in subsequent
34*894a1ed8SRobert Mustacchicore dumps, if generated.
35*894a1ed8SRobert Mustacchi.Lp
36*894a1ed8SRobert MustacchiThe
37*894a1ed8SRobert Mustacchi.Nm
38*894a1ed8SRobert Mustacchidata contained in each file has information about the layout and
39*894a1ed8SRobert Mustacchisizes of C types, including intrinsic types, enumerations, structures,
40*894a1ed8SRobert Mustacchitypedefs, and unions, that are used by the corresponding
41*894a1ed8SRobert Mustacchi.Sy ELF
42*894a1ed8SRobert Mustacchiobject. The
43*894a1ed8SRobert Mustacchi.Nm
44*894a1ed8SRobert Mustacchidata may also include information about the types of global objects and
45*894a1ed8SRobert Mustacchithe return type and arguments of functions in the symbol table.
46*894a1ed8SRobert Mustacchi.Lp
47*894a1ed8SRobert MustacchiBecause a
48*894a1ed8SRobert Mustacchi.Nm
49*894a1ed8SRobert Mustacchifile is often embedded inside a file, rather than being a standalone
50*894a1ed8SRobert Mustacchifile itself, it may also be referred to as a
51*894a1ed8SRobert Mustacchi.Nm
52*894a1ed8SRobert Mustacchi.Sy container .
53*894a1ed8SRobert Mustacchi.Lp
54*894a1ed8SRobert MustacchiOn illumos systems,
55*894a1ed8SRobert Mustacchi.Nm
56*894a1ed8SRobert Mustacchidata is consumed by multiple programs. It can be used by the modular
57*894a1ed8SRobert Mustacchidebugger,
58*894a1ed8SRobert Mustacchi.Xr mdb 1 ,
59*894a1ed8SRobert Mustacchias well as by
60*894a1ed8SRobert Mustacchi.Xr dtrace 1M .
61*894a1ed8SRobert MustacchiProgrammatic access to
62*894a1ed8SRobert Mustacchi.Nm
63*894a1ed8SRobert Mustacchidata can be obtained through
64*894a1ed8SRobert Mustacchi.Xr libctf 3LIB .
65*894a1ed8SRobert Mustacchi.Lp
66*894a1ed8SRobert MustacchiThe
67*894a1ed8SRobert Mustacchi.Nm
68*894a1ed8SRobert Mustacchifile format is broken down into seven different sections. The first
69*894a1ed8SRobert Mustacchisection is the
70*894a1ed8SRobert Mustacchi.Sy preamble
71*894a1ed8SRobert Mustacchiand
72*894a1ed8SRobert Mustacchi.Sy header ,
73*894a1ed8SRobert Mustacchiwhich describes the version of the
74*894a1ed8SRobert Mustacchi.Nm
75*894a1ed8SRobert Mustacchifile, links it has to other
76*894a1ed8SRobert Mustacchi.Nm
77*894a1ed8SRobert Mustacchifiles, and the sizes of the other sections. The next section is the
78*894a1ed8SRobert Mustacchi.Sy label
79*894a1ed8SRobert Mustacchisection,
80*894a1ed8SRobert Mustacchiwhich provides a way of identifying similar groups of
81*894a1ed8SRobert Mustacchi.Nm
82*894a1ed8SRobert Mustacchidata across multiple files. This is followed by the
83*894a1ed8SRobert Mustacchi.Sy object
84*894a1ed8SRobert Mustacchiinformation section, which describes the type of global
85*894a1ed8SRobert Mustacchisymbols. The subsequent section is the
86*894a1ed8SRobert Mustacchi.Sy function
87*894a1ed8SRobert Mustacchiinformation section, which describes the return
88*894a1ed8SRobert Mustacchitypes and arguments of functions. The next section is the
89*894a1ed8SRobert Mustacchi.Sy type
90*894a1ed8SRobert Mustacchiinformation section, which describes
91*894a1ed8SRobert Mustacchithe format and layout of the C types themselves, and finally the last
92*894a1ed8SRobert Mustacchisection is the
93*894a1ed8SRobert Mustacchi.Sy string
94*894a1ed8SRobert Mustacchisection, which contains the names of types, enumerations, members, and
95*894a1ed8SRobert Mustacchilabels.
96*894a1ed8SRobert Mustacchi.Lp
97*894a1ed8SRobert MustacchiWhile strictly speaking, only the
98*894a1ed8SRobert Mustacchi.Sy preamble
99*894a1ed8SRobert Mustacchiand
100*894a1ed8SRobert Mustacchi.Sy header
101*894a1ed8SRobert Mustacchiare required, to be actually useful, both the type and string
102*894a1ed8SRobert Mustacchisections are necessary.
103*894a1ed8SRobert Mustacchi.Lp
104*894a1ed8SRobert MustacchiA
105*894a1ed8SRobert Mustacchi.Nm
106*894a1ed8SRobert Mustacchifile may contain all of the type information that it requires, or it
107*894a1ed8SRobert Mustacchimay optionally refer to another
108*894a1ed8SRobert Mustacchi.Nm
109*894a1ed8SRobert Mustacchifile which holds the remaining types. When a
110*894a1ed8SRobert Mustacchi.Nm
111*894a1ed8SRobert Mustacchifile refers to another file, it is called the
112*894a1ed8SRobert Mustacchi.Sy child
113*894a1ed8SRobert Mustacchiand the file it refers to is called the
114*894a1ed8SRobert Mustacchi.Sy parent .
115*894a1ed8SRobert MustacchiA given file may only refer to one parent. This process is called
116*894a1ed8SRobert Mustacchi.Em uniquification
117*894a1ed8SRobert Mustacchibecause it ensures each child only has type information that is
118*894a1ed8SRobert Mustacchiunique to it. A common example of this is that most kernel modules in
119*894a1ed8SRobert Mustacchiillumos are uniquified against the kernel module
120*894a1ed8SRobert Mustacchi.Sy genunix
121*894a1ed8SRobert Mustacchiand the type information that comes from the
122*894a1ed8SRobert Mustacchi.Sy IP
123*894a1ed8SRobert Mustacchimodule. This means that a module only has types that are unique to
124*894a1ed8SRobert Mustacchiitself and the most common types in the kernel are not duplicated.
125*894a1ed8SRobert Mustacchi.Sh FILE FORMAT
126*894a1ed8SRobert MustacchiThis documents version
127*894a1ed8SRobert Mustacchi.Em two
128*894a1ed8SRobert Mustacchiof the
129*894a1ed8SRobert Mustacchi.Nm
130*894a1ed8SRobert Mustacchifile format. All applications and tools currently produce and operate on
131*894a1ed8SRobert Mustacchithis version.
132*894a1ed8SRobert Mustacchi.Lp
133*894a1ed8SRobert MustacchiThe file format can be summarized with the following image, the
134*894a1ed8SRobert Mustacchifollowing sections will cover this in more detail.
135*894a1ed8SRobert Mustacchi.Bd -literal
136*894a1ed8SRobert Mustacchi
137*894a1ed8SRobert Mustacchi         +-------------+  0t0
138*894a1ed8SRobert Mustacchi+--------| Preamble    |
139*894a1ed8SRobert Mustacchi|        +-------------+  0t4
140*894a1ed8SRobert Mustacchi|+-------| Header      |
141*894a1ed8SRobert Mustacchi||       +-------------+  0t36 + cth_lbloff
142*894a1ed8SRobert Mustacchi||+------| Labels      |
143*894a1ed8SRobert Mustacchi|||      +-------------+  0t36 + cth_objtoff
144*894a1ed8SRobert Mustacchi|||+-----| Objects     |
145*894a1ed8SRobert Mustacchi||||     +-------------+  0t36 + cth_funcoff
146*894a1ed8SRobert Mustacchi||||+----| Functions   |
147*894a1ed8SRobert Mustacchi|||||    +-------------+  0t36 + cth_typeoff
148*894a1ed8SRobert Mustacchi|||||+---| Types       |
149*894a1ed8SRobert Mustacchi||||||   +-------------+  0t36 + cth_stroff
150*894a1ed8SRobert Mustacchi||||||+--| Strings     |
151*894a1ed8SRobert Mustacchi|||||||  +-------------+  0t36 + cth_stroff + cth_strlen
152*894a1ed8SRobert Mustacchi|||||||
153*894a1ed8SRobert Mustacchi|||||||
154*894a1ed8SRobert Mustacchi|||||||
155*894a1ed8SRobert Mustacchi|||||||    +-- magic -   vers   flags
156*894a1ed8SRobert Mustacchi|||||||    |          |    |      |
157*894a1ed8SRobert Mustacchi|||||||   +------+------+------+------+
158*894a1ed8SRobert Mustacchi+---------| 0xcf | 0xf1 | 0x02 | 0x00 |
159*894a1ed8SRobert Mustacchi ||||||   +------+------+------+------+
160*894a1ed8SRobert Mustacchi ||||||   0      1      2      3      4
161*894a1ed8SRobert Mustacchi ||||||
162*894a1ed8SRobert Mustacchi ||||||    + parent label        + objects
163*894a1ed8SRobert Mustacchi ||||||    |       + parent name |     + functions    + strings
164*894a1ed8SRobert Mustacchi ||||||    |       |     + label |     |      + types |       + strlen
165*894a1ed8SRobert Mustacchi ||||||    |       |     |       |     |      |       |       |
166*894a1ed8SRobert Mustacchi ||||||   +------+------+------+------+------+-------+-------+-------+
167*894a1ed8SRobert Mustacchi +--------| 0x00 | 0x00 | 0x00 | 0x08 | 0x36 | 0x110 | 0x5f4 | 0x611 |
168*894a1ed8SRobert Mustacchi  |||||   +------+------+------+------+------+-------+-------+-------+
169*894a1ed8SRobert Mustacchi  |||||   0x04   0x08   0x0c   0x10   0x14    0x18    0x1c    0x20   0x24
170*894a1ed8SRobert Mustacchi  |||||
171*894a1ed8SRobert Mustacchi  |||||         + Label name
172*894a1ed8SRobert Mustacchi  |||||         |       + Label type
173*894a1ed8SRobert Mustacchi  |||||         |       |       + Next label
174*894a1ed8SRobert Mustacchi  |||||         |       |       |
175*894a1ed8SRobert Mustacchi  |||||       +-------+------+-----+
176*894a1ed8SRobert Mustacchi  +-----------| 0x01  | 0x42 | ... |
177*894a1ed8SRobert Mustacchi   ||||       +-------+------+-----+
178*894a1ed8SRobert Mustacchi   ||||  cth_lbloff   +0x4   +0x8  cth_objtoff
179*894a1ed8SRobert Mustacchi   ||||
180*894a1ed8SRobert Mustacchi   ||||
181*894a1ed8SRobert Mustacchi   |||| Symidx  0t15   0t43   0t44
182*894a1ed8SRobert Mustacchi   ||||       +------+------+------+-----+
183*894a1ed8SRobert Mustacchi   +----------| 0x00 | 0x42 | 0x36 | ... |
184*894a1ed8SRobert Mustacchi    |||       +------+------+------+-----+
185*894a1ed8SRobert Mustacchi    ||| cth_objtoff  +0x2   +0x4   +0x6   cth_funcoff
186*894a1ed8SRobert Mustacchi    |||
187*894a1ed8SRobert Mustacchi    |||        + CTF_TYPE_INFO         + CTF_TYPE_INFO
188*894a1ed8SRobert Mustacchi    |||        |        + Return type  |
189*894a1ed8SRobert Mustacchi    |||        |        |       + arg0 |
190*894a1ed8SRobert Mustacchi    |||       +--------+------+------+-----+
191*894a1ed8SRobert Mustacchi    +---------| 0x2c10 | 0x08 | 0x0c | ... |
192*894a1ed8SRobert Mustacchi     ||       +--------+------+------+-----+
193*894a1ed8SRobert Mustacchi     || cth_funcff     +0x2   +0x4   +0x6  cth_typeoff
194*894a1ed8SRobert Mustacchi     ||
195*894a1ed8SRobert Mustacchi     ||         + ctf_stype_t for type 1
196*894a1ed8SRobert Mustacchi     ||         |  integer           + integer encoding
197*894a1ed8SRobert Mustacchi     ||         |                    |          + ctf_stype_t for type 2
198*894a1ed8SRobert Mustacchi     ||         |                    |          |
199*894a1ed8SRobert Mustacchi     ||       +--------------------+-----------+-----+
200*894a1ed8SRobert Mustacchi     +--------| 0x19 * 0xc01 * 0x0 | 0x1000000 | ... |
201*894a1ed8SRobert Mustacchi      |       +--------------------+-----------+-----+
202*894a1ed8SRobert Mustacchi      | cth_typeoff               +0x08      +0x0c  cth_stroff
203*894a1ed8SRobert Mustacchi      |
204*894a1ed8SRobert Mustacchi      |     +--- str 0
205*894a1ed8SRobert Mustacchi      |     |    +--- str 1       + str 2
206*894a1ed8SRobert Mustacchi      |     |    |                |
207*894a1ed8SRobert Mustacchi      |     v    v                v
208*894a1ed8SRobert Mustacchi      |   +----+---+---+---+----+---+---+---+---+---+----+
209*894a1ed8SRobert Mustacchi      +---| \\0 | i | n | t | \\0 | f | o | o | _ | t | \\0 |
210*894a1ed8SRobert Mustacchi          +----+---+---+---+----+---+---+---+---+---+----+
211*894a1ed8SRobert Mustacchi          0    1   2   3   4    5   6   7   8   9   10   11
212*894a1ed8SRobert Mustacchi.Ed
213*894a1ed8SRobert Mustacchi.Lp
214*894a1ed8SRobert MustacchiEvery
215*894a1ed8SRobert Mustacchi.Nm
216*894a1ed8SRobert Mustacchifile begins with a
217*894a1ed8SRobert Mustacchi.Sy preamble ,
218*894a1ed8SRobert Mustacchifollowed by a
219*894a1ed8SRobert Mustacchi.Sy header .
220*894a1ed8SRobert MustacchiThe
221*894a1ed8SRobert Mustacchi.Sy preamble
222*894a1ed8SRobert Mustacchiis defined as follows:
223*894a1ed8SRobert Mustacchi.Bd -literal
224*894a1ed8SRobert Mustacchitypedef struct ctf_preamble {
225*894a1ed8SRobert Mustacchi	ushort_t ctp_magic;	/* magic number (CTF_MAGIC) */
226*894a1ed8SRobert Mustacchi	uchar_t ctp_version;	/* data format version number (CTF_VERSION) */
227*894a1ed8SRobert Mustacchi	uchar_t ctp_flags;	/* flags (see below) */
228*894a1ed8SRobert Mustacchi} ctf_preamble_t;
229*894a1ed8SRobert Mustacchi.Ed
230*894a1ed8SRobert Mustacchi.Pp
231*894a1ed8SRobert MustacchiThe
232*894a1ed8SRobert Mustacchi.Sy preamble
233*894a1ed8SRobert Mustacchiis four bytes long and must be four byte aligned.
234*894a1ed8SRobert MustacchiThis
235*894a1ed8SRobert Mustacchi.Sy preamble
236*894a1ed8SRobert Mustacchidefines the version of the
237*894a1ed8SRobert Mustacchi.Nm
238*894a1ed8SRobert Mustacchifile which defines the format of the rest of the header. While the
239*894a1ed8SRobert Mustacchiheader may change in subsequent versions, the preamble will not change
240*894a1ed8SRobert Mustacchiacross versions, though the interpretation of its flags may change from
241*894a1ed8SRobert Mustacchiversion to version. The
242*894a1ed8SRobert Mustacchi.Em ctp_magic
243*894a1ed8SRobert Mustacchimember defines the magic number for the
244*894a1ed8SRobert Mustacchi.Nm
245*894a1ed8SRobert Mustacchifile format. This must always be
246*894a1ed8SRobert Mustacchi.Li 0xcff1 .
247*894a1ed8SRobert MustacchiIf another value is encountered, then the file should not be treated as
248*894a1ed8SRobert Mustacchia
249*894a1ed8SRobert Mustacchi.Nm
250*894a1ed8SRobert Mustacchifile. The
251*894a1ed8SRobert Mustacchi.Em ctp_version
252*894a1ed8SRobert Mustacchimember defines the version of the
253*894a1ed8SRobert Mustacchi.Nm
254*894a1ed8SRobert Mustacchifile. The current version is
255*894a1ed8SRobert Mustacchi.Li 2 .
256*894a1ed8SRobert MustacchiIt is possible to encounter an unsupported version. In that case,
257*894a1ed8SRobert Mustacchisoftware should not try to parse the format, as it may have changed.
258*894a1ed8SRobert MustacchiFinally, the
259*894a1ed8SRobert Mustacchi.Em ctp_flags
260*894a1ed8SRobert Mustacchimember describes aspects of the file which modify its interpretation.
261*894a1ed8SRobert MustacchiThe following flags are currently defined:
262*894a1ed8SRobert Mustacchi.Bd -literal
263*894a1ed8SRobert Mustacchi#define	CTF_F_COMPRESS		0x01
264*894a1ed8SRobert Mustacchi.Ed
265*894a1ed8SRobert Mustacchi.Pp
266*894a1ed8SRobert MustacchiThe flag
267*894a1ed8SRobert Mustacchi.Sy CTF_F_COMPRESS
268*894a1ed8SRobert Mustacchiindicates that the body of the
269*894a1ed8SRobert Mustacchi.Nm
270*894a1ed8SRobert Mustacchifile, all the data following the
271*894a1ed8SRobert Mustacchi.Sy header ,
272*894a1ed8SRobert Mustacchihas been compressed through the
273*894a1ed8SRobert Mustacchi.Sy zlib
274*894a1ed8SRobert Mustacchilibrary and its
275*894a1ed8SRobert Mustacchi.Sy deflate
276*894a1ed8SRobert Mustacchialgorithm. If this flag is not present, then the body has not been
277*894a1ed8SRobert Mustacchicompressed and no special action is needed to interpret it. All offsets
278*894a1ed8SRobert Mustacchiinto the data as described by
279*894a1ed8SRobert Mustacchi.Sy header ,
280*894a1ed8SRobert Mustacchialways refer to the
281*894a1ed8SRobert Mustacchi.Sy uncompressed
282*894a1ed8SRobert Mustacchidata.
283*894a1ed8SRobert Mustacchi.Lp
284*894a1ed8SRobert MustacchiIn version two of the
285*894a1ed8SRobert Mustacchi.Nm
286*894a1ed8SRobert Mustacchifile format, the
287*894a1ed8SRobert Mustacchi.Sy header
288*894a1ed8SRobert Mustacchidenotes whether whether or not this
289*894a1ed8SRobert Mustacchi.Nm
290*894a1ed8SRobert Mustacchifile is the child of another
291*894a1ed8SRobert Mustacchi.Nm
292*894a1ed8SRobert Mustacchifile and also indicates the size of the remaining sections. The
293*894a1ed8SRobert Mustacchistructure for the
294*894a1ed8SRobert Mustacchi.Sy header ,
295*894a1ed8SRobert Mustacchilogically contains a copy of the
296*894a1ed8SRobert Mustacchi.Sy preamble
297*894a1ed8SRobert Mustacchiand the two have a combined size of 36 bytes.
298*894a1ed8SRobert Mustacchi.Bd -literal
299*894a1ed8SRobert Mustacchitypedef struct ctf_header {
300*894a1ed8SRobert Mustacchi	ctf_preamble_t cth_preamble;
301*894a1ed8SRobert Mustacchi	uint_t cth_parlabel;	/* ref to name of parent lbl uniq'd against */
302*894a1ed8SRobert Mustacchi	uint_t cth_parname;	/* ref to basename of parent */
303*894a1ed8SRobert Mustacchi	uint_t cth_lbloff;	/* offset of label section */
304*894a1ed8SRobert Mustacchi	uint_t cth_objtoff;	/* offset of object section */
305*894a1ed8SRobert Mustacchi	uint_t cth_funcoff;	/* offset of function section */
306*894a1ed8SRobert Mustacchi	uint_t cth_typeoff;	/* offset of type section */
307*894a1ed8SRobert Mustacchi	uint_t cth_stroff;	/* offset of string section */
308*894a1ed8SRobert Mustacchi	uint_t cth_strlen;	/* length of string section in bytes */
309*894a1ed8SRobert Mustacchi} ctf_header_t;
310*894a1ed8SRobert Mustacchi.Ed
311*894a1ed8SRobert Mustacchi.Pp
312*894a1ed8SRobert MustacchiAfter the
313*894a1ed8SRobert Mustacchi.Sy preamble ,
314*894a1ed8SRobert Mustacchithe next two members
315*894a1ed8SRobert Mustacchi.Em cth_parlablel
316*894a1ed8SRobert Mustacchiand
317*894a1ed8SRobert Mustacchi.Em cth_parname ,
318*894a1ed8SRobert Mustacchiare used to identify the parent. The value of both members are offsets
319*894a1ed8SRobert Mustacchiinto the
320*894a1ed8SRobert Mustacchi.Sy string
321*894a1ed8SRobert Mustacchisection which point to the start of a null-terminated string. For more
322*894a1ed8SRobert Mustacchiinformation on the encoding of strings, see the subsection on
323*894a1ed8SRobert Mustacchi.Sx String Identifiers .
324*894a1ed8SRobert MustacchiIf the value of either is zero, then there is no entry for that
325*894a1ed8SRobert Mustacchimember. If the member
326*894a1ed8SRobert Mustacchi.Em cth_parlabel
327*894a1ed8SRobert Mustacchiis set, then the
328*894a1ed8SRobert Mustacchi.Em ctf_parname
329*894a1ed8SRobert Mustacchimember must be set, otherwise it will not be possible to find the
330*894a1ed8SRobert Mustacchiparent. If
331*894a1ed8SRobert Mustacchi.Em ctf_parname
332*894a1ed8SRobert Mustacchiis set, it is not necessary to define
333*894a1ed8SRobert Mustacchi.Em cth_parlabel ,
334*894a1ed8SRobert Mustacchias the parent may not have a label. For more information on labels
335*894a1ed8SRobert Mustacchiand their interpretation, see
336*894a1ed8SRobert Mustacchi.Sx The Label Section .
337*894a1ed8SRobert Mustacchi.Lp
338*894a1ed8SRobert MustacchiThe remaining members (excepting
339*894a1ed8SRobert Mustacchi.Em cth_strlen )
340*894a1ed8SRobert Mustacchidescribe the beginning of the corresponding sections. These offsets are
341*894a1ed8SRobert Mustacchirelative to the end of the
342*894a1ed8SRobert Mustacchi.Sy header .
343*894a1ed8SRobert MustacchiTherefore, something with an offset of 0 is at an offset of thirty-six
344*894a1ed8SRobert Mustacchibytes relative to the start of the
345*894a1ed8SRobert Mustacchi.Nm
346*894a1ed8SRobert Mustacchifile. The difference between members
347*894a1ed8SRobert Mustacchiindicates the size of the section itself. Different offsets have
348*894a1ed8SRobert Mustacchidifferent alignment requirements. The start of the
349*894a1ed8SRobert Mustacchi.Em cth_objotoff
350*894a1ed8SRobert Mustacchiand
351*894a1ed8SRobert Mustacchi.Em cth_funcoff
352*894a1ed8SRobert Mustacchimust be two byte aligned, while the sections
353*894a1ed8SRobert Mustacchi.Em cth_lbloff
354*894a1ed8SRobert Mustacchiand
355*894a1ed8SRobert Mustacchi.Em cth_typeoff
356*894a1ed8SRobert Mustacchimust be four-byte aligned. The section
357*894a1ed8SRobert Mustacchi.Em cth_stroff
358*894a1ed8SRobert Mustacchihas no alignment requirements. To calculate the size of a given section,
359*894a1ed8SRobert Mustacchiexcepting the
360*894a1ed8SRobert Mustacchi.Sy string
361*894a1ed8SRobert Mustacchisection, one should subtract the offset of the section from the following one. For
362*894a1ed8SRobert Mustacchiexample, the size of the
363*894a1ed8SRobert Mustacchi.Sy types
364*894a1ed8SRobert Mustacchisection can be calculated by subtracting
365*894a1ed8SRobert Mustacchi.Em cth_stroff
366*894a1ed8SRobert Mustacchifrom
367*894a1ed8SRobert Mustacchi.Em cth_typeoff .
368*894a1ed8SRobert Mustacchi.Lp
369*894a1ed8SRobert MustacchiFinally, the member
370*894a1ed8SRobert Mustacchi.Em cth_strlen
371*894a1ed8SRobert Mustacchidescribes the length of the string section itself. From it, you can also
372*894a1ed8SRobert Mustacchicalculate the size of the entire
373*894a1ed8SRobert Mustacchi.Nm
374*894a1ed8SRobert Mustacchifile by adding together the size of the
375*894a1ed8SRobert Mustacchi.Sy ctf_header_t ,
376*894a1ed8SRobert Mustacchithe offset of the string section in
377*894a1ed8SRobert Mustacchi.Em cth_stroff ,
378*894a1ed8SRobert Mustacchiand the size of the string section in
379*894a1ed8SRobert Mustacchi.Em cth_srlen .
380*894a1ed8SRobert Mustacchi.Ss Type Identifiers
381*894a1ed8SRobert MustacchiThrough the
382*894a1ed8SRobert Mustacchi.Nm ctf
383*894a1ed8SRobert Mustacchidata, types are referred to by identifiers. A given
384*894a1ed8SRobert Mustacchi.Nm
385*894a1ed8SRobert Mustacchifile supports up to 32767 (0x7fff) types. The first valid type identifier is 0x1.
386*894a1ed8SRobert MustacchiWhen a given
387*894a1ed8SRobert Mustacchi.Nm
388*894a1ed8SRobert Mustacchifile is a child, indicated by a non-zero entry for the
389*894a1ed8SRobert Mustacchi.Sy header Ns 's
390*894a1ed8SRobert Mustacchi.Em cth_parname ,
391*894a1ed8SRobert Mustacchithen the first valid type identifier is 0x8000 and the last is 0xffff.
392*894a1ed8SRobert MustacchiIn this case, type identifiers 0x1 through 0x7fff are references to the
393*894a1ed8SRobert Mustacchiparent.
394*894a1ed8SRobert Mustacchi.Lp
395*894a1ed8SRobert MustacchiThe type identifier zero is a sentinel value used to indicate that there
396*894a1ed8SRobert Mustacchiis no type information available or it is an unknown type.
397*894a1ed8SRobert Mustacchi.Lp
398*894a1ed8SRobert MustacchiThroughout the file format, the identifier is stored in different sized
399*894a1ed8SRobert Mustacchivalues; however, the minimum size to represent a given identifier is a
400*894a1ed8SRobert Mustacchi.Sy uint16_t .
401*894a1ed8SRobert MustacchiOther consumers of
402*894a1ed8SRobert Mustacchi.Nm
403*894a1ed8SRobert Mustacchiinformation may use larger or opaque identifiers.
404*894a1ed8SRobert Mustacchi.Ss String Identifiers
405*894a1ed8SRobert MustacchiString identifiers are always encoded as four byte unsigned integers
406*894a1ed8SRobert Mustacchiwhich are an offset into a string table. The
407*894a1ed8SRobert Mustacchi.Nm
408*894a1ed8SRobert Mustacchiformat supports two different string tables which have an identifier of
409*894a1ed8SRobert Mustacchizero or one. This identifier is stored in the high-order bit of the
410*894a1ed8SRobert Mustacchiunsigned four byte offset. Therefore, the maximum supported offset into
411*894a1ed8SRobert Mustacchione of these tables is 0x7ffffffff.
412*894a1ed8SRobert Mustacchi.Lp
413*894a1ed8SRobert MustacchiTable identifier zero, always refers to the
414*894a1ed8SRobert Mustacchi.Sy string
415*894a1ed8SRobert Mustacchisection in the CTF file itself. String table identifier one refers to an
416*894a1ed8SRobert Mustacchiexternal string table which is the ELF string table for the ELF symbol
417*894a1ed8SRobert Mustacchitable associated with the
418*894a1ed8SRobert Mustacchi.Nm
419*894a1ed8SRobert Mustacchicontainer.
420*894a1ed8SRobert Mustacchi.Ss Type Encoding
421*894a1ed8SRobert MustacchiEvery
422*894a1ed8SRobert Mustacchi.Nm
423*894a1ed8SRobert Mustacchitype begins with metadata encoded into a
424*894a1ed8SRobert Mustacchi.Sy uint16_t .
425*894a1ed8SRobert MustacchiThis encoded information tells us three different pieces of information:
426*894a1ed8SRobert Mustacchi.Bl -bullet -offset indent -compact
427*894a1ed8SRobert Mustacchi.It
428*894a1ed8SRobert MustacchiThe kind of the type
429*894a1ed8SRobert Mustacchi.It
430*894a1ed8SRobert MustacchiWhether this type is a root type or not
431*894a1ed8SRobert Mustacchi.It
432*894a1ed8SRobert MustacchiThe length of the variable data
433*894a1ed8SRobert Mustacchi.El
434*894a1ed8SRobert Mustacchi.Lp
435*894a1ed8SRobert MustacchiThe 16 bits that make up the encoding are broken down such that you have
436*894a1ed8SRobert Mustacchifive bits for the kind, one bit for indicating whether or not it is a
437*894a1ed8SRobert Mustacchiroot type, and 10 bits for the variable length. This is laid out as
438*894a1ed8SRobert Mustacchifollows:
439*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
440*894a1ed8SRobert Mustacchi+--------------------+
441*894a1ed8SRobert Mustacchi| kind | root | vlen |
442*894a1ed8SRobert Mustacchi+--------------------+
443*894a1ed8SRobert Mustacchi15   11   10   9    0
444*894a1ed8SRobert Mustacchi.Ed
445*894a1ed8SRobert Mustacchi.Lp
446*894a1ed8SRobert MustacchiThe current version of the file format defines 14 different kinds. The
447*894a1ed8SRobert Mustacchiinterpretation of these different kinds will be discussed in the section
448*894a1ed8SRobert Mustacchi.Sx The Type Section .
449*894a1ed8SRobert MustacchiIf a kind is encountered that is not listed below, then it is not a valid
450*894a1ed8SRobert Mustacchi.Nm
451*894a1ed8SRobert Mustacchifile. The kinds are defined as follows:
452*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
453*894a1ed8SRobert Mustacchi#define	CTF_K_UNKNOWN	0
454*894a1ed8SRobert Mustacchi#define	CTF_K_INTEGER	1
455*894a1ed8SRobert Mustacchi#define	CTF_K_FLOAT	2
456*894a1ed8SRobert Mustacchi#define	CTF_K_POINTER	3
457*894a1ed8SRobert Mustacchi#define	CTF_K_ARRAY	4
458*894a1ed8SRobert Mustacchi#define	CTF_K_FUNCTION	5
459*894a1ed8SRobert Mustacchi#define	CTF_K_STRUCT	6
460*894a1ed8SRobert Mustacchi#define	CTF_K_UNION	7
461*894a1ed8SRobert Mustacchi#define	CTF_K_ENUM	8
462*894a1ed8SRobert Mustacchi#define	CTF_K_FORWARD	9
463*894a1ed8SRobert Mustacchi#define	CTF_K_TYPEDEF	10
464*894a1ed8SRobert Mustacchi#define	CTF_K_VOLATILE	11
465*894a1ed8SRobert Mustacchi#define	CTF_K_CONST	12
466*894a1ed8SRobert Mustacchi#define	CTF_K_RESTRICT	13
467*894a1ed8SRobert Mustacchi.Ed
468*894a1ed8SRobert Mustacchi.Lp
469*894a1ed8SRobert MustacchiPrograms directly reference many types; however, other types are referenced
470*894a1ed8SRobert Mustacchiindirectly because they are part of some other structure. These types that are
471*894a1ed8SRobert Mustacchireferenced directly and used are called
472*894a1ed8SRobert Mustacchi.Sy root
473*894a1ed8SRobert Mustacchitypes. Other types may be used indirectly, for example, a program may reference
474*894a1ed8SRobert Mustacchia structure directly, but not one of its members which has a type. That type is
475*894a1ed8SRobert Mustacchinot considered a
476*894a1ed8SRobert Mustacchi.Sy root
477*894a1ed8SRobert Mustacchitype. If a type is a
478*894a1ed8SRobert Mustacchi.Sy root
479*894a1ed8SRobert Mustacchitype, then it will have bit 10 set.
480*894a1ed8SRobert Mustacchi.Lp
481*894a1ed8SRobert MustacchiThe variable length section is specific to each kind and is discussed in the
482*894a1ed8SRobert Mustacchisection
483*894a1ed8SRobert Mustacchi.Sx The Type Section .
484*894a1ed8SRobert Mustacchi.Lp
485*894a1ed8SRobert MustacchiThe following macros are useful for constructing and deconstructing the encoded
486*894a1ed8SRobert Mustacchitype information:
487*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
488*894a1ed8SRobert Mustacchi
489*894a1ed8SRobert Mustacchi#define	CTF_MAX_VLEN	0x3ff
490*894a1ed8SRobert Mustacchi#define	CTF_INFO_KIND(info)	(((info) & 0xf800) >> 11)
491*894a1ed8SRobert Mustacchi#define	CTF_INFO_ISROOT(info)	(((info) & 0x0400) >> 10)
492*894a1ed8SRobert Mustacchi#define	CTF_INFO_VLEN(info)	(((info) & CTF_MAX_VLEN))
493*894a1ed8SRobert Mustacchi
494*894a1ed8SRobert Mustacchi#define	CTF_TYPE_INFO(kind, isroot, vlen) \\
495*894a1ed8SRobert Mustacchi	(((kind) << 11) | (((isroot) ? 1 : 0) << 10) | ((vlen) & CTF_MAX_VLEN))
496*894a1ed8SRobert Mustacchi.Ed
497*894a1ed8SRobert Mustacchi.Ss The Label Section
498*894a1ed8SRobert MustacchiWhen consuming
499*894a1ed8SRobert Mustacchi.Nm
500*894a1ed8SRobert Mustacchidata, it is often useful to know whether two different
501*894a1ed8SRobert Mustacchi.Nm
502*894a1ed8SRobert Mustacchicontainers come from the same source base and version. For example, when
503*894a1ed8SRobert Mustacchibuilding illumos, there are many kernel modules that are built against a
504*894a1ed8SRobert Mustacchisingle collection of source code. A label is encoded into the
505*894a1ed8SRobert Mustacchi.Nm
506*894a1ed8SRobert Mustacchifiles that corresponds with the particular build. This ensures that if
507*894a1ed8SRobert Mustacchifiles on the system were to become mixed up from multiple releases, that
508*894a1ed8SRobert Mustacchithey are not used together by tools, particularly when a child needs to
509*894a1ed8SRobert Mustacchirefer to a type in the parent. Because they are linked used the type
510*894a1ed8SRobert Mustacchiidentifiers, if the wrong parent is used then the wrong type will be
511*894a1ed8SRobert Mustacchiencountered.
512*894a1ed8SRobert Mustacchi.Lp
513*894a1ed8SRobert MustacchiEach label is encoded in the file format using the following eight byte
514*894a1ed8SRobert Mustacchistructure:
515*894a1ed8SRobert Mustacchi.Bd -literal
516*894a1ed8SRobert Mustacchitypedef struct ctf_lblent {
517*894a1ed8SRobert Mustacchi	uint_t ctl_label;	/* ref to name of label */
518*894a1ed8SRobert Mustacchi	uint_t ctl_typeidx;	/* last type associated with this label */
519*894a1ed8SRobert Mustacchi} ctf_lblent_t;
520*894a1ed8SRobert Mustacchi.Ed
521*894a1ed8SRobert Mustacchi.Lp
522*894a1ed8SRobert MustacchiEach label has two different components, a name and a type identifier.
523*894a1ed8SRobert MustacchiThe name is encoded in the
524*894a1ed8SRobert Mustacchi.Em ctl_label
525*894a1ed8SRobert Mustacchimember which is in the format defined in the section
526*894a1ed8SRobert Mustacchi.Sx String Identifiers .
527*894a1ed8SRobert MustacchiGenerally, the names of all labels are found in the internal string
528*894a1ed8SRobert Mustacchisection.
529*894a1ed8SRobert Mustacchi.Lp
530*894a1ed8SRobert MustacchiThe type identifier encoded in the member
531*894a1ed8SRobert Mustacchi.Em ctl_typeidx
532*894a1ed8SRobert Mustacchirefers to the last type identifier that a label refers to in the current
533*894a1ed8SRobert Mustacchifile. Labels only refer to types in the current file, if the
534*894a1ed8SRobert Mustacchi.Nm
535*894a1ed8SRobert Mustacchifile is a child, then it will have the same label as its parent;
536*894a1ed8SRobert Mustacchihowever, its label will only refer to its types, not its parents.
537*894a1ed8SRobert Mustacchi.Lp
538*894a1ed8SRobert MustacchiIt is also possible, though rather uncommon, for a
539*894a1ed8SRobert Mustacchi.Nm
540*894a1ed8SRobert Mustacchifile to have multiple labels. Labels are placed one after another, every
541*894a1ed8SRobert Mustacchieight bytes. When multiple labels are present, types may only belong to
542*894a1ed8SRobert Mustacchia single label.
543*894a1ed8SRobert Mustacchi.Ss The Object Section
544*894a1ed8SRobert MustacchiThe object section provides a mapping from ELF symbols of type
545*894a1ed8SRobert Mustacchi.Sy STT_OBJECT
546*894a1ed8SRobert Mustacchiin the symbol table to a type identifier. Every entry in this section is
547*894a1ed8SRobert Mustacchia
548*894a1ed8SRobert Mustacchi.Sy uint16_t
549*894a1ed8SRobert Mustacchiwhich contains a type identifier as described in the section
550*894a1ed8SRobert Mustacchi.Sx Type Identifiers .
551*894a1ed8SRobert MustacchiIf there is no information for an object, then the type identifier 0x0
552*894a1ed8SRobert Mustacchiis stored for that entry.
553*894a1ed8SRobert Mustacchi.Lp
554*894a1ed8SRobert MustacchiTo walk the object section, you need to have a corresponding
555*894a1ed8SRobert Mustacchi.Sy symbol table
556*894a1ed8SRobert Mustacchiin the ELF object that contains the
557*894a1ed8SRobert Mustacchi.Nm
558*894a1ed8SRobert Mustacchidata. Not every object is included in this section. Specifically, when
559*894a1ed8SRobert Mustacchiwalking the symbol table. An entry is skipped if it matches any of the
560*894a1ed8SRobert Mustacchifollowing conditions:
561*894a1ed8SRobert Mustacchi.Lp
562*894a1ed8SRobert Mustacchi.Bl -bullet -offset indent -compact
563*894a1ed8SRobert Mustacchi.It
564*894a1ed8SRobert MustacchiThe symbol type is not
565*894a1ed8SRobert Mustacchi.Sy STT_OBJECT
566*894a1ed8SRobert Mustacchi.It
567*894a1ed8SRobert MustacchiThe symbol's section index is
568*894a1ed8SRobert Mustacchi.Sy SHN_UNDEF
569*894a1ed8SRobert Mustacchi.It
570*894a1ed8SRobert MustacchiThe symbol's name offset is zero
571*894a1ed8SRobert Mustacchi.It
572*894a1ed8SRobert MustacchiThe symbol's section index is
573*894a1ed8SRobert Mustacchi.Sy SHN_ABS
574*894a1ed8SRobert Mustacchiand the value of the symbol is zero.
575*894a1ed8SRobert Mustacchi.It
576*894a1ed8SRobert MustacchiThe symbol's name is
577*894a1ed8SRobert Mustacchi.Li _START_
578*894a1ed8SRobert Mustacchior
579*894a1ed8SRobert Mustacchi.Li _END_ .
580*894a1ed8SRobert MustacchiThese are skipped because they are used for scoping local symbols in
581*894a1ed8SRobert MustacchiELF.
582*894a1ed8SRobert Mustacchi.El
583*894a1ed8SRobert Mustacchi.Lp
584*894a1ed8SRobert MustacchiThe following sample code shows an example of iterating the object
585*894a1ed8SRobert Mustacchisection and skipping the correct symbols:
586*894a1ed8SRobert Mustacchi.Bd -literal
587*894a1ed8SRobert Mustacchi#include <gelf.h>
588*894a1ed8SRobert Mustacchi#include <stdio.h>
589*894a1ed8SRobert Mustacchi
590*894a1ed8SRobert Mustacchi/*
591*894a1ed8SRobert Mustacchi * Given the start of the object section in the CTF file, the number of symbols,
592*894a1ed8SRobert Mustacchi * and the ELF Data sections for the symbol table and the string table, this
593*894a1ed8SRobert Mustacchi * prints the type identifiers that correspond to objects. Note, a more robust
594*894a1ed8SRobert Mustacchi * implementation should ensure that they don't walk beyond the end of the CTF
595*894a1ed8SRobert Mustacchi * object section.
596*894a1ed8SRobert Mustacchi */
597*894a1ed8SRobert Mustacchistatic int
598*894a1ed8SRobert Mustacchiwalk_symbols(uint16_t *objtoff, Elf_Data *symdata, Elf_Data *strdata,
599*894a1ed8SRobert Mustacchi    long nsyms)
600*894a1ed8SRobert Mustacchi{
601*894a1ed8SRobert Mustacchi	long i;
602*894a1ed8SRobert Mustacchi	uintptr_t strbase = strdata->d_buf;
603*894a1ed8SRobert Mustacchi
604*894a1ed8SRobert Mustacchi	for (i = 1; i < nsyms; i++, objftoff++) {
605*894a1ed8SRobert Mustacchi		const char *name;
606*894a1ed8SRobert Mustacchi		GElf_Sym sym;
607*894a1ed8SRobert Mustacchi
608*894a1ed8SRobert Mustacchi		if (gelf_getsym(symdata, i, &sym) == NULL)
609*894a1ed8SRobert Mustacchi			return (1);
610*894a1ed8SRobert Mustacchi
611*894a1ed8SRobert Mustacchi		if (GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
612*894a1ed8SRobert Mustacchi			continue;
613*894a1ed8SRobert Mustacchi		if (sym.st_shndx == SHN_UNDEF || sym.st_name == 0)
614*894a1ed8SRobert Mustacchi			continue;
615*894a1ed8SRobert Mustacchi		if (sym.st_shndx == SHN_ABS && sym.st_value == 0)
616*894a1ed8SRobert Mustacchi			continue;
617*894a1ed8SRobert Mustacchi		name = (const char *)(strbase + sym.st_name);
618*894a1ed8SRobert Mustacchi		if (strcmp(name, "_START_") == 0 || strcmp(name, "_END_") == 0)
619*894a1ed8SRobert Mustacchi			continue;
620*894a1ed8SRobert Mustacchi
621*894a1ed8SRobert Mustacchi		(void) printf("Symbol %d has type %d\n", i, *objtoff);
622*894a1ed8SRobert Mustacchi	}
623*894a1ed8SRobert Mustacchi
624*894a1ed8SRobert Mustacchi	return (0);
625*894a1ed8SRobert Mustacchi}
626*894a1ed8SRobert Mustacchi.Ed
627*894a1ed8SRobert Mustacchi.Ss The Function Section
628*894a1ed8SRobert MustacchiThe function section of the
629*894a1ed8SRobert Mustacchi.Nm
630*894a1ed8SRobert Mustacchifile encodes the types of both the function's arguments and the function's
631*894a1ed8SRobert Mustacchireturn type. Similar to
632*894a1ed8SRobert Mustacchi.Sx The Object Section ,
633*894a1ed8SRobert Mustacchithe function section encodes information for all symbols of type
634*894a1ed8SRobert Mustacchi.Sy STT_FUNCTION ,
635*894a1ed8SRobert Mustacchiexcepting those that fit specific criteria. Unlike with objects, because
636*894a1ed8SRobert Mustacchifunctions have a variable number of arguments, they start with a type encoding
637*894a1ed8SRobert Mustacchias defined in
638*894a1ed8SRobert Mustacchi.Sx Type Encoding ,
639*894a1ed8SRobert Mustacchiwhich is the size of a
640*894a1ed8SRobert Mustacchi.Sy uint16_t .
641*894a1ed8SRobert MustacchiFor functions which have no type information available, they are encoded as
642*894a1ed8SRobert Mustacchi.Li CTF_TYPE_INFO(CTF_K_UNKNOWN, 0, 0) .
643*894a1ed8SRobert MustacchiFunctions with arguments are encoded differently. Here, the variable length is
644*894a1ed8SRobert Mustacchiturned into the number of arguments in the function. If a function is a
645*894a1ed8SRobert Mustacchi.Sy varargs
646*894a1ed8SRobert Mustacchitype function, then the number of arguments is increased by one. Functions with
647*894a1ed8SRobert Mustacchitype information are encoded as:
648*894a1ed8SRobert Mustacchi.Li CTF_TYPE_INFO(CTF_K_FUNCTION, 0, nargs) .
649*894a1ed8SRobert Mustacchi.Lp
650*894a1ed8SRobert MustacchiFor functions that have no type information, nothing else is encoded, and the
651*894a1ed8SRobert Mustacchinext function is encoded. For functions with type information, the next
652*894a1ed8SRobert Mustacchi.Sy uint16_t
653*894a1ed8SRobert Mustacchiis encoded with the type identifier of the return type of the function. It is
654*894a1ed8SRobert Mustacchifollowed by each of the type identifiers of the arguments, if any exist, in the
655*894a1ed8SRobert Mustacchiorder that they appear in the function.  Therefore, argument 0 is the first type
656*894a1ed8SRobert Mustacchiidentifier and so on. When a function has a final varargs argument, that is
657*894a1ed8SRobert Mustacchiencoded with the type identifier of zero.
658*894a1ed8SRobert Mustacchi.Lp
659*894a1ed8SRobert MustacchiLike
660*894a1ed8SRobert Mustacchi.Sx The Object Section ,
661*894a1ed8SRobert Mustacchithe function section is encoded in the order of the symbol table. It has
662*894a1ed8SRobert Mustacchisimilar, but slightly different considerations from objects. While iterating the
663*894a1ed8SRobert Mustacchisymbol table, if any of the following conditions are true, then the entry is
664*894a1ed8SRobert Mustacchiskipped and no corresponding entry is written:
665*894a1ed8SRobert Mustacchi.Lp
666*894a1ed8SRobert Mustacchi.Bl -bullet -offset indent -compact
667*894a1ed8SRobert Mustacchi.It
668*894a1ed8SRobert MustacchiThe symbol type is not
669*894a1ed8SRobert Mustacchi.Sy STT_FUNCTION
670*894a1ed8SRobert Mustacchi.It
671*894a1ed8SRobert MustacchiThe symbol's section index is
672*894a1ed8SRobert Mustacchi.Sy SHN_UNDEF
673*894a1ed8SRobert Mustacchi.It
674*894a1ed8SRobert MustacchiThe symbol's name offset is zero
675*894a1ed8SRobert Mustacchi.It
676*894a1ed8SRobert MustacchiThe symbol's name is
677*894a1ed8SRobert Mustacchi.Li _START_
678*894a1ed8SRobert Mustacchior
679*894a1ed8SRobert Mustacchi.Li _END_ .
680*894a1ed8SRobert MustacchiThese are skipped because they are used for scoping local symbols in
681*894a1ed8SRobert MustacchiELF.
682*894a1ed8SRobert Mustacchi.El
683*894a1ed8SRobert Mustacchi.Ss The Type Section
684*894a1ed8SRobert MustacchiThe type section is the heart of the
685*894a1ed8SRobert Mustacchi.Nm
686*894a1ed8SRobert Mustacchidata. It encodes all of the information about the types themselves. The base of
687*894a1ed8SRobert Mustacchithe type information comes in two forms, a short form and a long form, each of
688*894a1ed8SRobert Mustacchiwhich may be followed by a variable number of arguments. The following
689*894a1ed8SRobert Mustacchidefinitions describe the short and long forms:
690*894a1ed8SRobert Mustacchi.Bd -literal
691*894a1ed8SRobert Mustacchi#define	CTF_MAX_SIZE	0xfffe	/* max size of a type in bytes */
692*894a1ed8SRobert Mustacchi#define	CTF_LSIZE_SENT	0xffff	/* sentinel for ctt_size */
693*894a1ed8SRobert Mustacchi#define	CTF_MAX_LSIZE	UINT64_MAX
694*894a1ed8SRobert Mustacchi
695*894a1ed8SRobert Mustacchitypedef struct ctf_stype {
696*894a1ed8SRobert Mustacchi	uint_t ctt_name;	/* reference to name in string table */
697*894a1ed8SRobert Mustacchi	ushort_t ctt_info;	/* encoded kind, variant length */
698*894a1ed8SRobert Mustacchi	union {
699*894a1ed8SRobert Mustacchi		ushort_t _size;	/* size of entire type in bytes */
700*894a1ed8SRobert Mustacchi		ushort_t _type;	/* reference to another type */
701*894a1ed8SRobert Mustacchi	} _u;
702*894a1ed8SRobert Mustacchi} ctf_stype_t;
703*894a1ed8SRobert Mustacchi
704*894a1ed8SRobert Mustacchitypedef struct ctf_type {
705*894a1ed8SRobert Mustacchi	uint_t ctt_name;	/* reference to name in string table */
706*894a1ed8SRobert Mustacchi	ushort_t ctt_info;	/* encoded kind, variant length */
707*894a1ed8SRobert Mustacchi	union {
708*894a1ed8SRobert Mustacchi		ushort_t _size;	/* always CTF_LSIZE_SENT */
709*894a1ed8SRobert Mustacchi		ushort_t _type; /* do not use */
710*894a1ed8SRobert Mustacchi	} _u;
711*894a1ed8SRobert Mustacchi	uint_t ctt_lsizehi;	/* high 32 bits of type size in bytes */
712*894a1ed8SRobert Mustacchi	uint_t ctt_lsizelo;	/* low 32 bits of type size in bytes */
713*894a1ed8SRobert Mustacchi} ctf_type_t;
714*894a1ed8SRobert Mustacchi
715*894a1ed8SRobert Mustacchi#define	ctt_size _u._size	/* for fundamental types that have a size */
716*894a1ed8SRobert Mustacchi#define	ctt_type _u._type	/* for types that reference another type */
717*894a1ed8SRobert Mustacchi.Ed
718*894a1ed8SRobert Mustacchi.Pp
719*894a1ed8SRobert MustacchiType sizes are stored in
720*894a1ed8SRobert Mustacchi.Sy bytes .
721*894a1ed8SRobert MustacchiThe basic small form uses a
722*894a1ed8SRobert Mustacchi.Sy ushort_t
723*894a1ed8SRobert Mustacchito store the number of bytes. If the number of bytes in a structure would exceed
724*894a1ed8SRobert Mustacchi0xfffe, then the alternate form, the
725*894a1ed8SRobert Mustacchi.Sy ctf_type_t ,
726*894a1ed8SRobert Mustacchiis used instead. To indicate that the larger form is being used, the member
727*894a1ed8SRobert Mustacchi.Em ctt_size
728*894a1ed8SRobert Mustacchiis set to value of
729*894a1ed8SRobert Mustacchi.Sy CTF_LSIZE_SENT
730*894a1ed8SRobert Mustacchi(0xffff). In general, when going through the type section, consumers use the
731*894a1ed8SRobert Mustacchi.Sy ctf_type_t
732*894a1ed8SRobert Mustacchistructure, but pay attention to the value of the member
733*894a1ed8SRobert Mustacchi.Em ctt_size
734*894a1ed8SRobert Mustacchito determine whether they should increment their scan by the size of the
735*894a1ed8SRobert Mustacchi.Sy ctf_stype_t
736*894a1ed8SRobert Mustacchior
737*894a1ed8SRobert Mustacchi.Sy ctf_type_t .
738*894a1ed8SRobert MustacchiNot all kinds of types use
739*894a1ed8SRobert Mustacchi.Sy ctt_size .
740*894a1ed8SRobert MustacchiThose which do not, will always use the
741*894a1ed8SRobert Mustacchi.Sy ctf_stype_t
742*894a1ed8SRobert Mustacchistructure. The individual sections for each kind have more information.
743*894a1ed8SRobert Mustacchi.Lp
744*894a1ed8SRobert MustacchiTypes are written out in order. Therefore the first entry encountered has a type
745*894a1ed8SRobert Mustacchiid of 0x1, or 0x8000 if a child. The member
746*894a1ed8SRobert Mustacchi.Em ctt_name
747*894a1ed8SRobert Mustacchiis encoded as described in the section
748*894a1ed8SRobert Mustacchi.Sx String Identifiers .
749*894a1ed8SRobert MustacchiThe string that it points to is the name of the type. If the identifier points
750*894a1ed8SRobert Mustacchito an empty string (one that consists solely of a null terminator) then the type
751*894a1ed8SRobert Mustacchidoes not have a name, this is common with anonymous structures and unions that
752*894a1ed8SRobert Mustacchionly have a typedef to name them, as well as, pointers and qualifiers.
753*894a1ed8SRobert Mustacchi.Lp
754*894a1ed8SRobert MustacchiThe next member, the
755*894a1ed8SRobert Mustacchi.Em ctt_info ,
756*894a1ed8SRobert Mustacchiis encoded as described in the section
757*894a1ed8SRobert Mustacchi.Sx Type Encoding .
758*894a1ed8SRobert MustacchiThe types kind tells us how to interpret the remaining data in the
759*894a1ed8SRobert Mustacchi.Sy ctf_type_t
760*894a1ed8SRobert Mustacchiand any variable length data that may exist. The rest of this section will be
761*894a1ed8SRobert Mustacchibroken down into the interpretation of the various kinds.
762*894a1ed8SRobert Mustacchi.Ss Encoding of Integers
763*894a1ed8SRobert MustacchiIntegers, which are of type
764*894a1ed8SRobert Mustacchi.Sy CTF_K_INTEGER ,
765*894a1ed8SRobert Mustacchihave no variable length arguments. Instead, they are followed by a four byte
766*894a1ed8SRobert Mustacchi.Sy uint_t
767*894a1ed8SRobert Mustacchiwhich describes their encoding. All integers must be encoded with a variable
768*894a1ed8SRobert Mustacchilength of zero. The
769*894a1ed8SRobert Mustacchi.Em ctt_size
770*894a1ed8SRobert Mustacchimember describes the length of the integer in bytes. In general, integer sizes
771*894a1ed8SRobert Mustacchiwill be rounded up to the closest power of two.
772*894a1ed8SRobert Mustacchi.Lp
773*894a1ed8SRobert MustacchiThe integer encoding contains three different pieces of information:
774*894a1ed8SRobert Mustacchi.Bl -bullet -offset indent -compact
775*894a1ed8SRobert Mustacchi.It
776*894a1ed8SRobert MustacchiThe encoding of the integer
777*894a1ed8SRobert Mustacchi.It
778*894a1ed8SRobert MustacchiThe offset in
779*894a1ed8SRobert Mustacchi.Sy bits
780*894a1ed8SRobert Mustacchiof the type
781*894a1ed8SRobert Mustacchi.It
782*894a1ed8SRobert MustacchiThe size in
783*894a1ed8SRobert Mustacchi.Sy bits
784*894a1ed8SRobert Mustacchiof the type
785*894a1ed8SRobert Mustacchi.El
786*894a1ed8SRobert Mustacchi.Pp
787*894a1ed8SRobert MustacchiThis encoding can be expressed through the following macros:
788*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
789*894a1ed8SRobert Mustacchi#define	CTF_INT_ENCODING(data)	(((data) & 0xff000000) >> 24)
790*894a1ed8SRobert Mustacchi#define	CTF_INT_OFFSET(data)	(((data) & 0x00ff0000) >> 16)
791*894a1ed8SRobert Mustacchi#define	CTF_INT_BITS(data)	(((data) & 0x0000ffff))
792*894a1ed8SRobert Mustacchi
793*894a1ed8SRobert Mustacchi#define	CTF_INT_DATA(encoding, offset, bits) \\
794*894a1ed8SRobert Mustacchi	(((encoding) << 24) | ((offset) << 16) | (bits))
795*894a1ed8SRobert Mustacchi.Ed
796*894a1ed8SRobert Mustacchi.Pp
797*894a1ed8SRobert MustacchiThe following flags are defined for the encoding at this time:
798*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
799*894a1ed8SRobert Mustacchi#define	CTF_INT_SIGNED		0x01
800*894a1ed8SRobert Mustacchi#define	CTF_INT_CHAR		0x02
801*894a1ed8SRobert Mustacchi#define	CTF_INT_BOOL		0x04
802*894a1ed8SRobert Mustacchi#define	CTF_INT_VARARGS		0x08
803*894a1ed8SRobert Mustacchi.Ed
804*894a1ed8SRobert Mustacchi.Lp
805*894a1ed8SRobert MustacchiBy default, an integer is considered to be unsigned, unless it has the
806*894a1ed8SRobert Mustacchi.Sy CTF_INT_SIGNED
807*894a1ed8SRobert Mustacchiflag set. If the flag
808*894a1ed8SRobert Mustacchi.Sy CTF_INT_CHAR
809*894a1ed8SRobert Mustacchiis set, that indicates that the integer is of a type that stores character
810*894a1ed8SRobert Mustacchidata, for example the intrinsic C type
811*894a1ed8SRobert Mustacchi.Sy char
812*894a1ed8SRobert Mustacchiwould have the
813*894a1ed8SRobert Mustacchi.Sy CTF_INT_CHAR
814*894a1ed8SRobert Mustacchiflag set. If the flag
815*894a1ed8SRobert Mustacchi.Sy CTF_INT_BOOL
816*894a1ed8SRobert Mustacchiis set, that indicates that the integer represents a boolean type. For example,
817*894a1ed8SRobert Mustacchithe intrinsic C type
818*894a1ed8SRobert Mustacchi.Sy _Bool
819*894a1ed8SRobert Mustacchiwould have the
820*894a1ed8SRobert Mustacchi.Sy CTF_INT_BOOL
821*894a1ed8SRobert Mustacchiflag set. Finally, the flag
822*894a1ed8SRobert Mustacchi.Sy CTF_INT_VARARGS
823*894a1ed8SRobert Mustacchiindicates that the integer is used as part of a variable number of arguments.
824*894a1ed8SRobert MustacchiThis encoding is rather uncommon.
825*894a1ed8SRobert Mustacchi.Ss Encoding of Floats
826*894a1ed8SRobert MustacchiFloats, which are of type
827*894a1ed8SRobert Mustacchi.Sy CTF_K_FLOAT ,
828*894a1ed8SRobert Mustacchiare similar to their integer counterparts. They have no variable length
829*894a1ed8SRobert Mustacchiarguments and are followed by a four byte encoding which describes the kind of
830*894a1ed8SRobert Mustacchifloat that exists. The
831*894a1ed8SRobert Mustacchi.Em ctt_size
832*894a1ed8SRobert Mustacchimember is the size, in bytes, of the float. The float encoding has three
833*894a1ed8SRobert Mustacchidifferent pieces of information inside of it:
834*894a1ed8SRobert Mustacchi.Lp
835*894a1ed8SRobert Mustacchi.Bl -bullet -offset indent -compact
836*894a1ed8SRobert Mustacchi.It
837*894a1ed8SRobert MustacchiThe specific kind of float that exists
838*894a1ed8SRobert Mustacchi.It
839*894a1ed8SRobert MustacchiThe offset in
840*894a1ed8SRobert Mustacchi.Sy bits
841*894a1ed8SRobert Mustacchiof the float
842*894a1ed8SRobert Mustacchi.It
843*894a1ed8SRobert MustacchiThe size in
844*894a1ed8SRobert Mustacchi.Sy bits
845*894a1ed8SRobert Mustacchiof the float
846*894a1ed8SRobert Mustacchi.El
847*894a1ed8SRobert Mustacchi.Lp
848*894a1ed8SRobert MustacchiThis encoding can be expressed through the following macros:
849*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
850*894a1ed8SRobert Mustacchi#define	CTF_FP_ENCODING(data)	(((data) & 0xff000000) >> 24)
851*894a1ed8SRobert Mustacchi#define	CTF_FP_OFFSET(data)	(((data) & 0x00ff0000) >> 16)
852*894a1ed8SRobert Mustacchi#define	CTF_FP_BITS(data)	(((data) & 0x0000ffff))
853*894a1ed8SRobert Mustacchi
854*894a1ed8SRobert Mustacchi#define	CTF_FP_DATA(encoding, offset, bits) \\
855*894a1ed8SRobert Mustacchi	(((encoding) << 24) | ((offset) << 16) | (bits))
856*894a1ed8SRobert Mustacchi.Ed
857*894a1ed8SRobert Mustacchi.Lp
858*894a1ed8SRobert MustacchiWhere as the encoding for integers was a series of flags, the encoding for
859*894a1ed8SRobert Mustacchifloats maps to a specific kind of float. It is not a flag-based value. The kinds of floats
860*894a1ed8SRobert Mustacchicorrespond to both their size, and the encoding. This covers all of the basic C
861*894a1ed8SRobert Mustacchiintrinsic floating point types. The following are the different kinds of floats
862*894a1ed8SRobert Mustacchirepresented in the encoding:
863*894a1ed8SRobert Mustacchi.Bd -literal -offset indent
864*894a1ed8SRobert Mustacchi#define	CTF_FP_SINGLE	1	/* IEEE 32-bit float encoding */
865*894a1ed8SRobert Mustacchi#define	CTF_FP_DOUBLE	2	/* IEEE 64-bit float encoding */
866*894a1ed8SRobert Mustacchi#define	CTF_FP_CPLX	3	/* Complex encoding */
867*894a1ed8SRobert Mustacchi#define	CTF_FP_DCPLX	4	/* Double complex encoding */
868*894a1ed8SRobert Mustacchi#define	CTF_FP_LDCPLX	5	/* Long double complex encoding */
869*894a1ed8SRobert Mustacchi#define	CTF_FP_LDOUBLE	6	/* Long double encoding */
870*894a1ed8SRobert Mustacchi#define	CTF_FP_INTRVL	7	/* Interval (2x32-bit) encoding */
871*894a1ed8SRobert Mustacchi#define	CTF_FP_DINTRVL	8	/* Double interval (2x64-bit) encoding */
872*894a1ed8SRobert Mustacchi#define	CTF_FP_LDINTRVL	9	/* Long double interval (2x128-bit) encoding */
873*894a1ed8SRobert Mustacchi#define	CTF_FP_IMAGRY	10	/* Imaginary (32-bit) encoding */
874*894a1ed8SRobert Mustacchi#define	CTF_FP_DIMAGRY	11	/* Long imaginary (64-bit) encoding */
875*894a1ed8SRobert Mustacchi#define	CTF_FP_LDIMAGRY	12	/* Long double imaginary (128-bit) encoding */
876*894a1ed8SRobert Mustacchi.Ed
877*894a1ed8SRobert Mustacchi.Ss Encoding of Arrays
878*894a1ed8SRobert MustacchiArrays, which are of type
879*894a1ed8SRobert Mustacchi.Sy CTF_K_ARRAY ,
880*894a1ed8SRobert Mustacchihave no variable length arguments. They are followed by a structure which
881*894a1ed8SRobert Mustacchidescribes the number of elements in the array, the type identifier of the
882*894a1ed8SRobert Mustacchielements in the array, and the type identifier of the index of the array. With
883*894a1ed8SRobert Mustacchiarrays, the
884*894a1ed8SRobert Mustacchi.Em ctt_size
885*894a1ed8SRobert Mustacchimember is set to zero. The structure that follows an array is defined as:
886*894a1ed8SRobert Mustacchi.Bd -literal
887*894a1ed8SRobert Mustacchitypedef struct ctf_array {
888*894a1ed8SRobert Mustacchi	ushort_t cta_contents;	/* reference to type of array contents */
889*894a1ed8SRobert Mustacchi	ushort_t cta_index;	/* reference to type of array index */
890*894a1ed8SRobert Mustacchi	uint_t cta_nelems;	/* number of elements */
891*894a1ed8SRobert Mustacchi} ctf_array_t;
892*894a1ed8SRobert Mustacchi.Ed
893*894a1ed8SRobert Mustacchi.Lp
894*894a1ed8SRobert MustacchiThe
895*894a1ed8SRobert Mustacchi.Em cta_contents
896*894a1ed8SRobert Mustacchiand
897*894a1ed8SRobert Mustacchi.Em cta_index
898*894a1ed8SRobert Mustacchimembers of the
899*894a1ed8SRobert Mustacchi.Sy ctf_array_t
900*894a1ed8SRobert Mustacchiare type identifiers which are encoded as per the section
901*894a1ed8SRobert Mustacchi.Sx Type Identifiers .
902*894a1ed8SRobert MustacchiThe member
903*894a1ed8SRobert Mustacchi.Em cta_nelems
904*894a1ed8SRobert Mustacchiis a simple four byte unsigned count of the number of elements. This count may
905*894a1ed8SRobert Mustacchibe zero when encountering C99's flexible array members.
906*894a1ed8SRobert Mustacchi.Ss Encoding of Functions
907*894a1ed8SRobert MustacchiFunction types, which are of type
908*894a1ed8SRobert Mustacchi.Sy CTF_K_FUNCTION ,
909*894a1ed8SRobert Mustacchiuse the variable length list to be the number of arguments in the function. When
910*894a1ed8SRobert Mustacchithe function has a final member which is a varargs, then the argument count is
911*894a1ed8SRobert Mustacchiincremented by one to account for the variable argument. Here, the
912*894a1ed8SRobert Mustacchi.Em ctt_type
913*894a1ed8SRobert Mustacchimember is encoded with the type identifier of the return type of the function.
914*894a1ed8SRobert MustacchiNote that the
915*894a1ed8SRobert Mustacchi.Em ctt_size
916*894a1ed8SRobert Mustacchimember is not used here.
917*894a1ed8SRobert Mustacchi.Lp
918*894a1ed8SRobert MustacchiThe variable argument list contains the type identifiers for the arguments of
919*894a1ed8SRobert Mustacchithe function, if any. Each one is represented by a
920*894a1ed8SRobert Mustacchi.Sy uint16_t
921*894a1ed8SRobert Mustacchiand encoded according to the
922*894a1ed8SRobert Mustacchi.Sx Type Identifiers
923*894a1ed8SRobert Mustacchisection. If the function's last argument is of type varargs, then it is also
924*894a1ed8SRobert Mustacchiwritten out, but the type identifier is zero. This is included in the count of
925*894a1ed8SRobert Mustacchithe function's arguments.
926*894a1ed8SRobert Mustacchi.Ss Encoding of Structures and Unions
927*894a1ed8SRobert MustacchiStructures and Unions, which are encoded with
928*894a1ed8SRobert Mustacchi.Sy CTF_K_STRUCT
929*894a1ed8SRobert Mustacchiand
930*894a1ed8SRobert Mustacchi.Sy CTF_K_UNION
931*894a1ed8SRobert Mustacchirespectively,  are very similar constructs in C. The main difference
932*894a1ed8SRobert Mustacchibetween them is the fact that every member of a structure follows one another,
933*894a1ed8SRobert Mustacchiwhere as in a union, all members share the same memory. They are also very
934*894a1ed8SRobert Mustacchisimilar in terms of their encoding in
935*894a1ed8SRobert Mustacchi.Nm .
936*894a1ed8SRobert MustacchiThe variable length argument for structures and unions represents the number of
937*894a1ed8SRobert Mustacchimembers that they have. The value of the member
938*894a1ed8SRobert Mustacchi.Em ctt_size
939*894a1ed8SRobert Mustacchiis the size of the structure and union. There are two different structures which
940*894a1ed8SRobert Mustacchiare used to encode members in the variable list. When the size of a structure or
941*894a1ed8SRobert Mustacchiunion is greater than or equal to the large member threshold, 8192, then a
942*894a1ed8SRobert Mustacchidifferent structure is used to encode the member, all members are encoded using
943*894a1ed8SRobert Mustacchithe same structure. The structure for members is as follows:
944*894a1ed8SRobert Mustacchi.Bd -literal
945*894a1ed8SRobert Mustacchitypedef struct ctf_member {
946*894a1ed8SRobert Mustacchi	uint_t ctm_name;	/* reference to name in string table */
947*894a1ed8SRobert Mustacchi	ushort_t ctm_type;	/* reference to type of member */
948*894a1ed8SRobert Mustacchi	ushort_t ctm_offset;	/* offset of this member in bits */
949*894a1ed8SRobert Mustacchi} ctf_member_t;
950*894a1ed8SRobert Mustacchi
951*894a1ed8SRobert Mustacchitypedef struct ctf_lmember {
952*894a1ed8SRobert Mustacchi	uint_t ctlm_name;	/* reference to name in string table */
953*894a1ed8SRobert Mustacchi	ushort_t ctlm_type;	/* reference to type of member */
954*894a1ed8SRobert Mustacchi	ushort_t ctlm_pad;	/* padding */
955*894a1ed8SRobert Mustacchi	uint_t ctlm_offsethi;	/* high 32 bits of member offset in bits */
956*894a1ed8SRobert Mustacchi	uint_t ctlm_offsetlo;	/* low 32 bits of member offset in bits */
957*894a1ed8SRobert Mustacchi} ctf_lmember_t;
958*894a1ed8SRobert Mustacchi.Ed
959*894a1ed8SRobert Mustacchi.Lp
960*894a1ed8SRobert MustacchiBoth the
961*894a1ed8SRobert Mustacchi.Em ctm_name
962*894a1ed8SRobert Mustacchiand
963*894a1ed8SRobert Mustacchi.Em ctlm_name
964*894a1ed8SRobert Mustacchirefer to the name of the member. The name is encoded as an offset into the
965*894a1ed8SRobert Mustacchistring table as described by the section
966*894a1ed8SRobert Mustacchi.Sx String Identifiers .
967*894a1ed8SRobert MustacchiThe members
968*894a1ed8SRobert Mustacchi.Sy ctm_type
969*894a1ed8SRobert Mustacchiand
970*894a1ed8SRobert Mustacchi.Sy ctlm_type
971*894a1ed8SRobert Mustacchiboth refer to the type of the member. They are encoded as per the section
972*894a1ed8SRobert Mustacchi.Sx Type Identifiers .
973*894a1ed8SRobert Mustacchi.Lp
974*894a1ed8SRobert MustacchiThe last piece of information that is present is the offset which describes the
975*894a1ed8SRobert Mustacchioffset in memory that the member begins at. For unions, this value will always
976*894a1ed8SRobert Mustacchibe zero because the start of unions in memory is always zero. For structures,
977*894a1ed8SRobert Mustacchithis is the offset in
978*894a1ed8SRobert Mustacchi.Sy bits
979*894a1ed8SRobert Mustacchithat the member begins at. Note that a compiler may lay out a type with padding.
980*894a1ed8SRobert MustacchiThis means that the difference in offset between two consecutive members may be
981*894a1ed8SRobert Mustacchilarger than the size of the member. When the size of the overall structure is
982*894a1ed8SRobert Mustacchistrictly less than 8192 bytes, the normal structure,
983*894a1ed8SRobert Mustacchi.Sy ctf_member_t ,
984*894a1ed8SRobert Mustacchiis used and the offset in bits is stored in the member
985*894a1ed8SRobert Mustacchi.Em ctm_offset .
986*894a1ed8SRobert MustacchiHowever, when the size of the structure is greater than or equal to 8192 bytes,
987*894a1ed8SRobert Mustacchithen the number of bits is split into two 32-bit quantities. One member,
988*894a1ed8SRobert Mustacchi.Em ctlm_offsethi ,
989*894a1ed8SRobert Mustacchirepresents the upper 32 bits of the offset, while the other member,
990*894a1ed8SRobert Mustacchi.Em ctlm_offsetlo ,
991*894a1ed8SRobert Mustacchirepresents the lower 32 bits of the offset. These can be joined together to get
992*894a1ed8SRobert Mustacchia 64-bit sized offset in bits by shifting the member
993*894a1ed8SRobert Mustacchi.Em ctlm_offsethi
994*894a1ed8SRobert Mustacchito the left by thirty two and then doing a binary or of
995*894a1ed8SRobert Mustacchi.Em ctlm_offsetlo .
996*894a1ed8SRobert Mustacchi.Ss Encoding of Enumerations
997*894a1ed8SRobert MustacchiEnumerations, noted by the type
998*894a1ed8SRobert Mustacchi.Sy CTF_K_ENUM ,
999*894a1ed8SRobert Mustacchiare similar to structures. Enumerations use the variable list to note the number
1000*894a1ed8SRobert Mustacchiof values that the enumeration contains, which we'll term enumerators. In C, an
1001*894a1ed8SRobert Mustacchienumeration is always equivalent to the intrinsic type
1002*894a1ed8SRobert Mustacchi.Sy int ,
1003*894a1ed8SRobert Mustacchithus the value of the member
1004*894a1ed8SRobert Mustacchi.Em ctt_size
1005*894a1ed8SRobert Mustacchiis always the size of an integer which is determined based on the current model.
1006*894a1ed8SRobert MustacchiFor illumos systems, this will always be 4, as an integer is always defined to
1007*894a1ed8SRobert Mustacchibe 4 bytes large in both
1008*894a1ed8SRobert Mustacchi.Sy ILP32
1009*894a1ed8SRobert Mustacchiand
1010*894a1ed8SRobert Mustacchi.Sy LP64 ,
1011*894a1ed8SRobert Mustacchiregardless of the architecture.
1012*894a1ed8SRobert Mustacchi.Lp
1013*894a1ed8SRobert MustacchiThe enumerators encoded in an enumeration have the following structure in the
1014*894a1ed8SRobert Mustacchivariable list:
1015*894a1ed8SRobert Mustacchi.Bd -literal
1016*894a1ed8SRobert Mustacchitypedef struct ctf_enum {
1017*894a1ed8SRobert Mustacchi	uint_t cte_name;	/* reference to name in string table */
1018*894a1ed8SRobert Mustacchi	int cte_value;		/* value associated with this name */
1019*894a1ed8SRobert Mustacchi} ctf_enum_t;
1020*894a1ed8SRobert Mustacchi.Ed
1021*894a1ed8SRobert Mustacchi.Pp
1022*894a1ed8SRobert MustacchiThe member
1023*894a1ed8SRobert Mustacchi.Em cte_name
1024*894a1ed8SRobert Mustacchirefers to the name of the enumerator's value, it is encoded according to the
1025*894a1ed8SRobert Mustacchirules in the section
1026*894a1ed8SRobert Mustacchi.Sx String Identifiers .
1027*894a1ed8SRobert MustacchiThe member
1028*894a1ed8SRobert Mustacchi.Em cte_value
1029*894a1ed8SRobert Mustacchicontains the integer value of this enumerator.
1030*894a1ed8SRobert Mustacchi.Ss Encoding of Forward References
1031*894a1ed8SRobert MustacchiForward references, types of kind
1032*894a1ed8SRobert Mustacchi.Sy CTF_K_FORWARD ,
1033*894a1ed8SRobert Mustacchiin a
1034*894a1ed8SRobert Mustacchi.Nm
1035*894a1ed8SRobert Mustacchifile refer to types which may not have a definition at all, only a name. If
1036*894a1ed8SRobert Mustacchithe
1037*894a1ed8SRobert Mustacchi.Nm
1038*894a1ed8SRobert Mustacchifile is a child, then it may be that the forward is resolved to an
1039*894a1ed8SRobert Mustacchiactual type in the parent, otherwise the definition may be in another
1040*894a1ed8SRobert Mustacchi.Nm
1041*894a1ed8SRobert Mustacchicontainer or may not be known at all. The only member of the
1042*894a1ed8SRobert Mustacchi.Sy ctf_type_t
1043*894a1ed8SRobert Mustacchithat matters for a forward declaration is the
1044*894a1ed8SRobert Mustacchi.Em ctt_name
1045*894a1ed8SRobert Mustacchiwhich points to the name of the forward reference in the string table as
1046*894a1ed8SRobert Mustacchidescribed earlier. There is no other information recorded for forward
1047*894a1ed8SRobert Mustacchireferences.
1048*894a1ed8SRobert Mustacchi.Ss Encoding of Pointers, Typedefs, Volatile, Const, and Restrict
1049*894a1ed8SRobert MustacchiPointers, typedefs, volatile, const, and restrict are all similar in
1050*894a1ed8SRobert Mustacchi.Nm .
1051*894a1ed8SRobert MustacchiThey all refer to another type. In the case of typedefs, they provide an
1052*894a1ed8SRobert Mustacchialternate name, while volatile, const, and restrict change how the type is
1053*894a1ed8SRobert Mustacchiinterpreted in the C programming language. This covers the
1054*894a1ed8SRobert Mustacchi.Nm
1055*894a1ed8SRobert Mustacchikinds
1056*894a1ed8SRobert Mustacchi.Sy CTF_K_POINTER ,
1057*894a1ed8SRobert Mustacchi.Sy CTF_K_TYPEDEF ,
1058*894a1ed8SRobert Mustacchi.Sy CTF_K_VOLATILE ,
1059*894a1ed8SRobert Mustacchi.Sy CTF_K_RESTRICT ,
1060*894a1ed8SRobert Mustacchiand
1061*894a1ed8SRobert Mustacchi.Sy CTF_K_CONST .
1062*894a1ed8SRobert Mustacchi.Lp
1063*894a1ed8SRobert MustacchiThese types have no variable list entries and use the member
1064*894a1ed8SRobert Mustacchi.Em ctt_type
1065*894a1ed8SRobert Mustacchito refer to the base type that they modify.
1066*894a1ed8SRobert Mustacchi.Ss Encoding of Unknown Types
1067*894a1ed8SRobert MustacchiTypes with the kind
1068*894a1ed8SRobert Mustacchi.Sy CTF_K_UNKNOWN
1069*894a1ed8SRobert Mustacchiare used to indicate gaps in the type identifier space. These entries consume an
1070*894a1ed8SRobert Mustacchiidentifier, but do not define anything. Nothing should refer to these gap
1071*894a1ed8SRobert Mustacchiidentifiers.
1072*894a1ed8SRobert Mustacchi.Ss Dependencies Between Types
1073*894a1ed8SRobert MustacchiC types can be imagined as a directed, cyclic, graph. Structures and unions may
1074*894a1ed8SRobert Mustacchirefer to each other in a way that creates a cyclic dependency. In cases such as
1075*894a1ed8SRobert Mustacchithese, the entire type section must be read in and processed. Consumers must
1076*894a1ed8SRobert Mustacchinot assume that every type can be laid out in dependency order; they
1077*894a1ed8SRobert Mustacchicannot.
1078*894a1ed8SRobert Mustacchi.Ss The String Section
1079*894a1ed8SRobert MustacchiThe last section of the
1080*894a1ed8SRobert Mustacchi.Nm
1081*894a1ed8SRobert Mustacchifile is the
1082*894a1ed8SRobert Mustacchi.Sy string
1083*894a1ed8SRobert Mustacchisection. This section encodes all of the strings that appear throughout
1084*894a1ed8SRobert Mustacchithe other sections. It is laid out as a series of characters followed by
1085*894a1ed8SRobert Mustacchia null terminator. Generally, all names are written out in ASCII, as
1086*894a1ed8SRobert Mustacchimost C compilers do not allow and characters to appear in identifiers
1087*894a1ed8SRobert Mustacchioutside of a subset of ASCII. However, any extended characters sets
1088*894a1ed8SRobert Mustacchishould be written out as a series of UTF-8 bytes.
1089*894a1ed8SRobert Mustacchi.Lp
1090*894a1ed8SRobert MustacchiThe first entry in the section, at offset zero, is a single null
1091*894a1ed8SRobert Mustacchiterminator to reference the empty string. Following that, each C string
1092*894a1ed8SRobert Mustacchishould be written out, including the null terminator. Offsets that refer
1093*894a1ed8SRobert Mustacchito something in this section should refer to the first byte which begins
1094*894a1ed8SRobert Mustacchia string. Beyond the first byte in the section being the null
1095*894a1ed8SRobert Mustacchiterminator, the order of strings is unimportant.
1096*894a1ed8SRobert Mustacchi.Sh Data Encoding and ELF Considerations
1097*894a1ed8SRobert Mustacchi.Nm
1098*894a1ed8SRobert Mustacchidata is generally included in ELF objects which specify information to
1099*894a1ed8SRobert Mustacchiidentify the architecture and endianness of the file. A
1100*894a1ed8SRobert Mustacchi.Nm
1101*894a1ed8SRobert Mustacchicontainer inside such an object must match the endianness of the ELF
1102*894a1ed8SRobert Mustacchiobject. Aside from the question of the endian encoding of data, there
1103*894a1ed8SRobert Mustacchishould be no other differences between architectures. While many of the
1104*894a1ed8SRobert Mustacchitypes in this document refer to non-fixed size C integral types, they
1105*894a1ed8SRobert Mustacchiare equivalent in the models
1106*894a1ed8SRobert Mustacchi.Sy ILP32
1107*894a1ed8SRobert Mustacchiand
1108*894a1ed8SRobert Mustacchi.Sy LP64 .
1109*894a1ed8SRobert MustacchiIf any other model is being used with
1110*894a1ed8SRobert Mustacchi.Nm
1111*894a1ed8SRobert Mustacchidata that has different sizes, then it must not use the model's sizes for
1112*894a1ed8SRobert Mustacchithose integral types and instead use the fixed size equivalents based on an
1113*894a1ed8SRobert Mustacchi.Sy ILP32
1114*894a1ed8SRobert Mustacchienvironment.
1115*894a1ed8SRobert Mustacchi.Lp
1116*894a1ed8SRobert MustacchiWhen placing a
1117*894a1ed8SRobert Mustacchi.Nm
1118*894a1ed8SRobert Mustacchicontainer inside of an ELF object, there are certain conventions that are
1119*894a1ed8SRobert Mustacchiexpected for the purposes of tooling being able to find the
1120*894a1ed8SRobert Mustacchi.Nm
1121*894a1ed8SRobert Mustacchidata. In particular, a given ELF object should only contain a single
1122*894a1ed8SRobert Mustacchi.Nm
1123*894a1ed8SRobert Mustacchisection. Multiple containers should be merged together into a single
1124*894a1ed8SRobert Mustacchione.
1125*894a1ed8SRobert Mustacchi.Lp
1126*894a1ed8SRobert MustacchiThe
1127*894a1ed8SRobert Mustacchi.Nm
1128*894a1ed8SRobert Mustacchifile should be included in its own ELF section. The section's name
1129*894a1ed8SRobert Mustacchimust be
1130*894a1ed8SRobert Mustacchi.Ql .SUNW_ctf .
1131*894a1ed8SRobert MustacchiThe type of the section must be
1132*894a1ed8SRobert Mustacchi.Sy SHT_PROGBITS .
1133*894a1ed8SRobert MustacchiThe section should have a link set to the symbol table and its address
1134*894a1ed8SRobert Mustacchialignment must be 4.
1135*894a1ed8SRobert Mustacchi.Sh SEE ALSO
1136*894a1ed8SRobert Mustacchi.Xr mdb 1 ,
1137*894a1ed8SRobert Mustacchi.Xr dtrace 1M ,
1138*894a1ed8SRobert Mustacchi.Xr libelf 3LIB ,
1139*894a1ed8SRobert Mustacchi.Xr gelf 3ELF ,
1140*894a1ed8SRobert Mustacchi.Xr a.out 4
1141