xref: /illumos-gate/usr/src/man/man5/ctf.5 (revision 11994f6f)
1fe2e029eSRobert Mustacchi.\"
2fe2e029eSRobert Mustacchi.\" This file and its contents are supplied under the terms of the
3fe2e029eSRobert Mustacchi.\" Common Development and Distribution License ("CDDL"), version 1.0.
4fe2e029eSRobert Mustacchi.\" You may only use this file in accordance with the terms of version
5fe2e029eSRobert Mustacchi.\" 1.0 of the CDDL.
6fe2e029eSRobert Mustacchi.\"
7fe2e029eSRobert Mustacchi.\" A full copy of the text of the CDDL should have accompanied this
8fe2e029eSRobert Mustacchi.\" source.  A copy of the CDDL is also available via the Internet at
9fe2e029eSRobert Mustacchi.\" http://www.illumos.org/license/CDDL.
10fe2e029eSRobert Mustacchi.\"
11fe2e029eSRobert Mustacchi.\"
12fe2e029eSRobert Mustacchi.\" Copyright (c) 2014 Joyent, Inc.
13fe2e029eSRobert Mustacchi.\"
14*11994f6fSRobert Mustacchi.Dd December 2, 2023
15bbf21555SRichard Lowe.Dt CTF 5
16fe2e029eSRobert Mustacchi.Os
17fe2e029eSRobert Mustacchi.Sh NAME
18fe2e029eSRobert Mustacchi.Nm ctf
19fe2e029eSRobert Mustacchi.Nd Compact C Type Format
20fe2e029eSRobert Mustacchi.Sh SYNOPSIS
21fe2e029eSRobert Mustacchi.In sys/ctf.h
22fe2e029eSRobert Mustacchi.Sh DESCRIPTION
23fe2e029eSRobert Mustacchi.Nm
24fe2e029eSRobert Mustacchiis designed to be a compact representation of the C programming
25fe2e029eSRobert Mustacchilanguage's type information focused on serving the needs of dynamic
26fe2e029eSRobert Mustacchitracing, debuggers, and other in-situ and post-mortem introspection
27fe2e029eSRobert Mustacchitools.
28fe2e029eSRobert Mustacchi.Nm
29fe2e029eSRobert Mustacchidata is generally included in
30fe2e029eSRobert Mustacchi.Sy ELF
31fe2e029eSRobert Mustacchiobjects and is tagged as
32fe2e029eSRobert Mustacchi.Sy SHT_PROGBITS
33fe2e029eSRobert Mustacchito ensure that the data is accessible in a running process and in subsequent
34fe2e029eSRobert Mustacchicore dumps, if generated.
35fe2e029eSRobert Mustacchi.Lp
36fe2e029eSRobert MustacchiThe
37fe2e029eSRobert Mustacchi.Nm
38fe2e029eSRobert Mustacchidata contained in each file has information about the layout and
39fe2e029eSRobert Mustacchisizes of C types, including intrinsic types, enumerations, structures,
40fe2e029eSRobert Mustacchitypedefs, and unions, that are used by the corresponding
41fe2e029eSRobert Mustacchi.Sy ELF
4272d3dbb9SYuri Pankovobject.
4372d3dbb9SYuri PankovThe
44fe2e029eSRobert Mustacchi.Nm
45fe2e029eSRobert Mustacchidata may also include information about the types of global objects and
46fe2e029eSRobert Mustacchithe return type and arguments of functions in the symbol table.
47fe2e029eSRobert Mustacchi.Lp
48fe2e029eSRobert MustacchiBecause a
49fe2e029eSRobert Mustacchi.Nm
50fe2e029eSRobert Mustacchifile is often embedded inside a file, rather than being a standalone
51fe2e029eSRobert Mustacchifile itself, it may also be referred to as a
52fe2e029eSRobert Mustacchi.Nm
53fe2e029eSRobert Mustacchi.Sy container .
54fe2e029eSRobert Mustacchi.Lp
55fe2e029eSRobert MustacchiOn illumos systems,
56fe2e029eSRobert Mustacchi.Nm
5772d3dbb9SYuri Pankovdata is consumed by multiple programs.
5872d3dbb9SYuri PankovIt can be used by the modular debugger,
59fe2e029eSRobert Mustacchi.Xr mdb 1 ,
60fe2e029eSRobert Mustacchias well as by
61bbf21555SRichard Lowe.Xr dtrace 8 .
62fe2e029eSRobert MustacchiProgrammatic access to
63fe2e029eSRobert Mustacchi.Nm
64fe2e029eSRobert Mustacchidata can be obtained through
65fe2e029eSRobert Mustacchi.Xr libctf 3LIB .
66fe2e029eSRobert Mustacchi.Lp
67fe2e029eSRobert MustacchiThe
68fe2e029eSRobert Mustacchi.Nm
6972d3dbb9SYuri Pankovfile format is broken down into seven different sections.
7072d3dbb9SYuri PankovThe first section is the
71fe2e029eSRobert Mustacchi.Sy preamble
72fe2e029eSRobert Mustacchiand
73fe2e029eSRobert Mustacchi.Sy header ,
74fe2e029eSRobert Mustacchiwhich describes the version of the
75fe2e029eSRobert Mustacchi.Nm
76fe2e029eSRobert Mustacchifile, links it has to other
77fe2e029eSRobert Mustacchi.Nm
7872d3dbb9SYuri Pankovfiles, and the sizes of the other sections.
7972d3dbb9SYuri PankovThe next section is the
80fe2e029eSRobert Mustacchi.Sy label
81fe2e029eSRobert Mustacchisection,
82fe2e029eSRobert Mustacchiwhich provides a way of identifying similar groups of
83fe2e029eSRobert Mustacchi.Nm
8472d3dbb9SYuri Pankovdata across multiple files.
8572d3dbb9SYuri PankovThis is followed by the
86fe2e029eSRobert Mustacchi.Sy object
87fe2e029eSRobert Mustacchiinformation section, which describes the type of global
8872d3dbb9SYuri Pankovsymbols.
8972d3dbb9SYuri PankovThe subsequent section is the
90fe2e029eSRobert Mustacchi.Sy function
91fe2e029eSRobert Mustacchiinformation section, which describes the return
9272d3dbb9SYuri Pankovtypes and arguments of functions.
9372d3dbb9SYuri PankovThe next section is the
94fe2e029eSRobert Mustacchi.Sy type
95fe2e029eSRobert Mustacchiinformation section, which describes
96fe2e029eSRobert Mustacchithe format and layout of the C types themselves, and finally the last
97fe2e029eSRobert Mustacchisection is the
98fe2e029eSRobert Mustacchi.Sy string
99fe2e029eSRobert Mustacchisection, which contains the names of types, enumerations, members, and
100fe2e029eSRobert Mustacchilabels.
101fe2e029eSRobert Mustacchi.Lp
102fe2e029eSRobert MustacchiWhile strictly speaking, only the
103fe2e029eSRobert Mustacchi.Sy preamble
104fe2e029eSRobert Mustacchiand
105fe2e029eSRobert Mustacchi.Sy header
106fe2e029eSRobert Mustacchiare required, to be actually useful, both the type and string
107fe2e029eSRobert Mustacchisections are necessary.
108fe2e029eSRobert Mustacchi.Lp
109fe2e029eSRobert MustacchiA
110fe2e029eSRobert Mustacchi.Nm
111fe2e029eSRobert Mustacchifile may contain all of the type information that it requires, or it
112fe2e029eSRobert Mustacchimay optionally refer to another
113fe2e029eSRobert Mustacchi.Nm
11472d3dbb9SYuri Pankovfile which holds the remaining types.
11572d3dbb9SYuri PankovWhen a
116fe2e029eSRobert Mustacchi.Nm
117fe2e029eSRobert Mustacchifile refers to another file, it is called the
118fe2e029eSRobert Mustacchi.Sy child
119fe2e029eSRobert Mustacchiand the file it refers to is called the
120fe2e029eSRobert Mustacchi.Sy parent .
12172d3dbb9SYuri PankovA given file may only refer to one parent.
12272d3dbb9SYuri PankovThis process is called
123fe2e029eSRobert Mustacchi.Em uniquification
124fe2e029eSRobert Mustacchibecause it ensures each child only has type information that is
12572d3dbb9SYuri Pankovunique to it.
12672d3dbb9SYuri PankovA common example of this is that most kernel modules in illumos are uniquified
12772d3dbb9SYuri Pankovagainst the kernel module
128fe2e029eSRobert Mustacchi.Sy genunix
129fe2e029eSRobert Mustacchiand the type information that comes from the
130fe2e029eSRobert Mustacchi.Sy IP
13172d3dbb9SYuri Pankovmodule.
13272d3dbb9SYuri PankovThis means that a module only has types that are unique to itself and the most
13372d3dbb9SYuri Pankovcommon types in the kernel are not duplicated.
134fe2e029eSRobert Mustacchi.Sh FILE FORMAT
135fe2e029eSRobert MustacchiThis documents version
136fe2e029eSRobert Mustacchi.Em two
137fe2e029eSRobert Mustacchiof the
138fe2e029eSRobert Mustacchi.Nm
13972d3dbb9SYuri Pankovfile format.
14072d3dbb9SYuri PankovAll applications and tools currently produce and operate on this version.
141fe2e029eSRobert Mustacchi.Lp
142fe2e029eSRobert MustacchiThe file format can be summarized with the following image, the
143fe2e029eSRobert Mustacchifollowing sections will cover this in more detail.
144fe2e029eSRobert Mustacchi.Bd -literal
145fe2e029eSRobert Mustacchi
146fe2e029eSRobert Mustacchi         +-------------+  0t0
147fe2e029eSRobert Mustacchi+--------| Preamble    |
148fe2e029eSRobert Mustacchi|        +-------------+  0t4
149fe2e029eSRobert Mustacchi|+-------| Header      |
150fe2e029eSRobert Mustacchi||       +-------------+  0t36 + cth_lbloff
151fe2e029eSRobert Mustacchi||+------| Labels      |
152fe2e029eSRobert Mustacchi|||      +-------------+  0t36 + cth_objtoff
153fe2e029eSRobert Mustacchi|||+-----| Objects     |
154fe2e029eSRobert Mustacchi||||     +-------------+  0t36 + cth_funcoff
155fe2e029eSRobert Mustacchi||||+----| Functions   |
156fe2e029eSRobert Mustacchi|||||    +-------------+  0t36 + cth_typeoff
157fe2e029eSRobert Mustacchi|||||+---| Types       |
158fe2e029eSRobert Mustacchi||||||   +-------------+  0t36 + cth_stroff
159fe2e029eSRobert Mustacchi||||||+--| Strings     |
160fe2e029eSRobert Mustacchi|||||||  +-------------+  0t36 + cth_stroff + cth_strlen
161fe2e029eSRobert Mustacchi|||||||
162fe2e029eSRobert Mustacchi|||||||
163fe2e029eSRobert Mustacchi|||||||
164fe2e029eSRobert Mustacchi|||||||    +-- magic -   vers   flags
165fe2e029eSRobert Mustacchi|||||||    |          |    |      |
166fe2e029eSRobert Mustacchi|||||||   +------+------+------+------+
167fe2e029eSRobert Mustacchi+---------| 0xcf | 0xf1 | 0x02 | 0x00 |
168fe2e029eSRobert Mustacchi ||||||   +------+------+------+------+
169fe2e029eSRobert Mustacchi ||||||   0      1      2      3      4
170fe2e029eSRobert Mustacchi ||||||
171fe2e029eSRobert Mustacchi ||||||    + parent label        + objects
172fe2e029eSRobert Mustacchi ||||||    |       + parent name |     + functions    + strings
173fe2e029eSRobert Mustacchi ||||||    |       |     + label |     |      + types |       + strlen
174fe2e029eSRobert Mustacchi ||||||    |       |     |       |     |      |       |       |
175fe2e029eSRobert Mustacchi ||||||   +------+------+------+------+------+-------+-------+-------+
176fe2e029eSRobert Mustacchi +--------| 0x00 | 0x00 | 0x00 | 0x08 | 0x36 | 0x110 | 0x5f4 | 0x611 |
177fe2e029eSRobert Mustacchi  |||||   +------+------+------+------+------+-------+-------+-------+
178fe2e029eSRobert Mustacchi  |||||   0x04   0x08   0x0c   0x10   0x14    0x18    0x1c    0x20   0x24
179fe2e029eSRobert Mustacchi  |||||
180fe2e029eSRobert Mustacchi  |||||         + Label name
181fe2e029eSRobert Mustacchi  |||||         |       + Label type
182fe2e029eSRobert Mustacchi  |||||         |       |       + Next label
183fe2e029eSRobert Mustacchi  |||||         |       |       |
184fe2e029eSRobert Mustacchi  |||||       +-------+------+-----+
185fe2e029eSRobert Mustacchi  +-----------| 0x01  | 0x42 | ... |
186fe2e029eSRobert Mustacchi   ||||       +-------+------+-----+
187fe2e029eSRobert Mustacchi   ||||  cth_lbloff   +0x4   +0x8  cth_objtoff
188fe2e029eSRobert Mustacchi   ||||
189fe2e029eSRobert Mustacchi   ||||
190fe2e029eSRobert Mustacchi   |||| Symidx  0t15   0t43   0t44
191fe2e029eSRobert Mustacchi   ||||       +------+------+------+-----+
192fe2e029eSRobert Mustacchi   +----------| 0x00 | 0x42 | 0x36 | ... |
193fe2e029eSRobert Mustacchi    |||       +------+------+------+-----+
194fe2e029eSRobert Mustacchi    ||| cth_objtoff  +0x2   +0x4   +0x6   cth_funcoff
195fe2e029eSRobert Mustacchi    |||
196fe2e029eSRobert Mustacchi    |||        + CTF_TYPE_INFO         + CTF_TYPE_INFO
197fe2e029eSRobert Mustacchi    |||        |        + Return type  |
198fe2e029eSRobert Mustacchi    |||        |        |       + arg0 |
199fe2e029eSRobert Mustacchi    |||       +--------+------+------+-----+
200fe2e029eSRobert Mustacchi    +---------| 0x2c10 | 0x08 | 0x0c | ... |
201fe2e029eSRobert Mustacchi     ||       +--------+------+------+-----+
202fe2e029eSRobert Mustacchi     || cth_funcff     +0x2   +0x4   +0x6  cth_typeoff
203fe2e029eSRobert Mustacchi     ||
204fe2e029eSRobert Mustacchi     ||         + ctf_stype_t for type 1
205fe2e029eSRobert Mustacchi     ||         |  integer           + integer encoding
206fe2e029eSRobert Mustacchi     ||         |                    |          + ctf_stype_t for type 2
207fe2e029eSRobert Mustacchi     ||         |                    |          |
208fe2e029eSRobert Mustacchi     ||       +--------------------+-----------+-----+
209fe2e029eSRobert Mustacchi     +--------| 0x19 * 0xc01 * 0x0 | 0x1000000 | ... |
210fe2e029eSRobert Mustacchi      |       +--------------------+-----------+-----+
211fe2e029eSRobert Mustacchi      | cth_typeoff               +0x08      +0x0c  cth_stroff
212fe2e029eSRobert Mustacchi      |
213fe2e029eSRobert Mustacchi      |     +--- str 0
214fe2e029eSRobert Mustacchi      |     |    +--- str 1       + str 2
215fe2e029eSRobert Mustacchi      |     |    |                |
216fe2e029eSRobert Mustacchi      |     v    v                v
217fe2e029eSRobert Mustacchi      |   +----+---+---+---+----+---+---+---+---+---+----+
218*11994f6fSRobert Mustacchi      +---| \e0 | i | n | t | \e0 | f | o | o | _ | t | \e0 |
219fe2e029eSRobert Mustacchi          +----+---+---+---+----+---+---+---+---+---+----+
220fe2e029eSRobert Mustacchi          0    1   2   3   4    5   6   7   8   9   10   11
221fe2e029eSRobert Mustacchi.Ed
222fe2e029eSRobert Mustacchi.Lp
223fe2e029eSRobert MustacchiEvery
224fe2e029eSRobert Mustacchi.Nm
225fe2e029eSRobert Mustacchifile begins with a
226fe2e029eSRobert Mustacchi.Sy preamble ,
227fe2e029eSRobert Mustacchifollowed by a
228fe2e029eSRobert Mustacchi.Sy header .
229fe2e029eSRobert MustacchiThe
230fe2e029eSRobert Mustacchi.Sy preamble
231fe2e029eSRobert Mustacchiis defined as follows:
232fe2e029eSRobert Mustacchi.Bd -literal
233fe2e029eSRobert Mustacchitypedef struct ctf_preamble {
234fe2e029eSRobert Mustacchi	ushort_t ctp_magic;	/* magic number (CTF_MAGIC) */
235fe2e029eSRobert Mustacchi	uchar_t ctp_version;	/* data format version number (CTF_VERSION) */
236fe2e029eSRobert Mustacchi	uchar_t ctp_flags;	/* flags (see below) */
237fe2e029eSRobert Mustacchi} ctf_preamble_t;
238fe2e029eSRobert Mustacchi.Ed
239fe2e029eSRobert Mustacchi.Pp
240fe2e029eSRobert MustacchiThe
241fe2e029eSRobert Mustacchi.Sy preamble
242fe2e029eSRobert Mustacchiis four bytes long and must be four byte aligned.
243fe2e029eSRobert MustacchiThis
244fe2e029eSRobert Mustacchi.Sy preamble
245fe2e029eSRobert Mustacchidefines the version of the
246fe2e029eSRobert Mustacchi.Nm
24772d3dbb9SYuri Pankovfile which defines the format of the rest of the header.
24872d3dbb9SYuri PankovWhile the header may change in subsequent versions, the preamble will not change
249fe2e029eSRobert Mustacchiacross versions, though the interpretation of its flags may change from
25072d3dbb9SYuri Pankovversion to version.
25172d3dbb9SYuri PankovThe
252fe2e029eSRobert Mustacchi.Em ctp_magic
253fe2e029eSRobert Mustacchimember defines the magic number for the
254fe2e029eSRobert Mustacchi.Nm
25572d3dbb9SYuri Pankovfile format.
25672d3dbb9SYuri PankovThis must always be
257fe2e029eSRobert Mustacchi.Li 0xcff1 .
258fe2e029eSRobert MustacchiIf another value is encountered, then the file should not be treated as
259fe2e029eSRobert Mustacchia
260fe2e029eSRobert Mustacchi.Nm
26172d3dbb9SYuri Pankovfile.
26272d3dbb9SYuri PankovThe
263fe2e029eSRobert Mustacchi.Em ctp_version
264fe2e029eSRobert Mustacchimember defines the version of the
265fe2e029eSRobert Mustacchi.Nm
26672d3dbb9SYuri Pankovfile.
26772d3dbb9SYuri PankovThe current version is
268fe2e029eSRobert Mustacchi.Li 2 .
26972d3dbb9SYuri PankovIt is possible to encounter an unsupported version.
27072d3dbb9SYuri PankovIn that case, software should not try to parse the format, as it may have
27172d3dbb9SYuri Pankovchanged.
272fe2e029eSRobert MustacchiFinally, the
273fe2e029eSRobert Mustacchi.Em ctp_flags
274fe2e029eSRobert Mustacchimember describes aspects of the file which modify its interpretation.
275fe2e029eSRobert MustacchiThe following flags are currently defined:
276fe2e029eSRobert Mustacchi.Bd -literal
277fe2e029eSRobert Mustacchi#define	CTF_F_COMPRESS		0x01
278fe2e029eSRobert Mustacchi.Ed
279fe2e029eSRobert Mustacchi.Pp
280fe2e029eSRobert MustacchiThe flag
281fe2e029eSRobert Mustacchi.Sy CTF_F_COMPRESS
282fe2e029eSRobert Mustacchiindicates that the body of the
283fe2e029eSRobert Mustacchi.Nm
284fe2e029eSRobert Mustacchifile, all the data following the
285fe2e029eSRobert Mustacchi.Sy header ,
286fe2e029eSRobert Mustacchihas been compressed through the
287fe2e029eSRobert Mustacchi.Sy zlib
288fe2e029eSRobert Mustacchilibrary and its
289fe2e029eSRobert Mustacchi.Sy deflate
29072d3dbb9SYuri Pankovalgorithm.
29172d3dbb9SYuri PankovIf this flag is not present, then the body has not been compressed and no
29272d3dbb9SYuri Pankovspecial action is needed to interpret it.
29372d3dbb9SYuri PankovAll offsets into the data as described by
294fe2e029eSRobert Mustacchi.Sy header ,
295fe2e029eSRobert Mustacchialways refer to the
296fe2e029eSRobert Mustacchi.Sy uncompressed
297fe2e029eSRobert Mustacchidata.
298fe2e029eSRobert Mustacchi.Lp
299fe2e029eSRobert MustacchiIn version two of the
300fe2e029eSRobert Mustacchi.Nm
301fe2e029eSRobert Mustacchifile format, the
302fe2e029eSRobert Mustacchi.Sy header
303fe2e029eSRobert Mustacchidenotes whether whether or not this
304fe2e029eSRobert Mustacchi.Nm
305fe2e029eSRobert Mustacchifile is the child of another
306fe2e029eSRobert Mustacchi.Nm
30772d3dbb9SYuri Pankovfile and also indicates the size of the remaining sections.
30872d3dbb9SYuri PankovThe structure for the
309fe2e029eSRobert Mustacchi.Sy header ,
310fe2e029eSRobert Mustacchilogically contains a copy of the
311fe2e029eSRobert Mustacchi.Sy preamble
312fe2e029eSRobert Mustacchiand the two have a combined size of 36 bytes.
313fe2e029eSRobert Mustacchi.Bd -literal
314fe2e029eSRobert Mustacchitypedef struct ctf_header {
315fe2e029eSRobert Mustacchi	ctf_preamble_t cth_preamble;
316fe2e029eSRobert Mustacchi	uint_t cth_parlabel;	/* ref to name of parent lbl uniq'd against */
317fe2e029eSRobert Mustacchi	uint_t cth_parname;	/* ref to basename of parent */
318fe2e029eSRobert Mustacchi	uint_t cth_lbloff;	/* offset of label section */
319fe2e029eSRobert Mustacchi	uint_t cth_objtoff;	/* offset of object section */
320fe2e029eSRobert Mustacchi	uint_t cth_funcoff;	/* offset of function section */
321fe2e029eSRobert Mustacchi	uint_t cth_typeoff;	/* offset of type section */
322fe2e029eSRobert Mustacchi	uint_t cth_stroff;	/* offset of string section */
323fe2e029eSRobert Mustacchi	uint_t cth_strlen;	/* length of string section in bytes */
324fe2e029eSRobert Mustacchi} ctf_header_t;
325fe2e029eSRobert Mustacchi.Ed
326fe2e029eSRobert Mustacchi.Pp
327fe2e029eSRobert MustacchiAfter the
328fe2e029eSRobert Mustacchi.Sy preamble ,
329fe2e029eSRobert Mustacchithe next two members
330fe2e029eSRobert Mustacchi.Em cth_parlablel
331fe2e029eSRobert Mustacchiand
332fe2e029eSRobert Mustacchi.Em cth_parname ,
33372d3dbb9SYuri Pankovare used to identify the parent.
33472d3dbb9SYuri PankovThe value of both members are offsets into the
335fe2e029eSRobert Mustacchi.Sy string
33672d3dbb9SYuri Pankovsection which point to the start of a null-terminated string.
33772d3dbb9SYuri PankovFor more information on the encoding of strings, see the subsection on
338fe2e029eSRobert Mustacchi.Sx String Identifiers .
339fe2e029eSRobert MustacchiIf the value of either is zero, then there is no entry for that
34072d3dbb9SYuri Pankovmember.
34172d3dbb9SYuri PankovIf the member
342fe2e029eSRobert Mustacchi.Em cth_parlabel
343fe2e029eSRobert Mustacchiis set, then the
344fe2e029eSRobert Mustacchi.Em ctf_parname
345fe2e029eSRobert Mustacchimember must be set, otherwise it will not be possible to find the
34672d3dbb9SYuri Pankovparent.
34772d3dbb9SYuri PankovIf
348fe2e029eSRobert Mustacchi.Em ctf_parname
349fe2e029eSRobert Mustacchiis set, it is not necessary to define
350fe2e029eSRobert Mustacchi.Em cth_parlabel ,
35172d3dbb9SYuri Pankovas the parent may not have a label.
35272d3dbb9SYuri PankovFor more information on labels and their interpretation, see
353fe2e029eSRobert Mustacchi.Sx The Label Section .
354fe2e029eSRobert Mustacchi.Lp
355fe2e029eSRobert MustacchiThe remaining members (excepting
356fe2e029eSRobert Mustacchi.Em cth_strlen )
35772d3dbb9SYuri Pankovdescribe the beginning of the corresponding sections.
35872d3dbb9SYuri PankovThese offsets are relative to the end of the
359fe2e029eSRobert Mustacchi.Sy header .
360fe2e029eSRobert MustacchiTherefore, something with an offset of 0 is at an offset of thirty-six
361fe2e029eSRobert Mustacchibytes relative to the start of the
362fe2e029eSRobert Mustacchi.Nm
36372d3dbb9SYuri Pankovfile.
36472d3dbb9SYuri PankovThe difference between members indicates the size of the section itself.
36572d3dbb9SYuri PankovDifferent offsets have different alignment requirements.
36672d3dbb9SYuri PankovThe start of the
367fe2e029eSRobert Mustacchi.Em cth_objotoff
368fe2e029eSRobert Mustacchiand
369fe2e029eSRobert Mustacchi.Em cth_funcoff
370fe2e029eSRobert Mustacchimust be two byte aligned, while the sections
371fe2e029eSRobert Mustacchi.Em cth_lbloff
372fe2e029eSRobert Mustacchiand
373fe2e029eSRobert Mustacchi.Em cth_typeoff
37472d3dbb9SYuri Pankovmust be four-byte aligned.
37572d3dbb9SYuri PankovThe section
376fe2e029eSRobert Mustacchi.Em cth_stroff
37772d3dbb9SYuri Pankovhas no alignment requirements.
37872d3dbb9SYuri PankovTo calculate the size of a given section, excepting the
379fe2e029eSRobert Mustacchi.Sy string
38072d3dbb9SYuri Pankovsection, one should subtract the offset of the section from the following one.
38172d3dbb9SYuri PankovFor example, the size of the
382fe2e029eSRobert Mustacchi.Sy types
383fe2e029eSRobert Mustacchisection can be calculated by subtracting
384fe2e029eSRobert Mustacchi.Em cth_stroff
385fe2e029eSRobert Mustacchifrom
386fe2e029eSRobert Mustacchi.Em cth_typeoff .
387fe2e029eSRobert Mustacchi.Lp
388fe2e029eSRobert MustacchiFinally, the member
389fe2e029eSRobert Mustacchi.Em cth_strlen
39072d3dbb9SYuri Pankovdescribes the length of the string section itself.
39172d3dbb9SYuri PankovFrom it, you can also calculate the size of the entire
392fe2e029eSRobert Mustacchi.Nm
393fe2e029eSRobert Mustacchifile by adding together the size of the
394fe2e029eSRobert Mustacchi.Sy ctf_header_t ,
395fe2e029eSRobert Mustacchithe offset of the string section in
396fe2e029eSRobert Mustacchi.Em cth_stroff ,
397fe2e029eSRobert Mustacchiand the size of the string section in
398fe2e029eSRobert Mustacchi.Em cth_srlen .
399fe2e029eSRobert Mustacchi.Ss Type Identifiers
400fe2e029eSRobert MustacchiThrough the
401fe2e029eSRobert Mustacchi.Nm ctf
40272d3dbb9SYuri Pankovdata, types are referred to by identifiers.
40372d3dbb9SYuri PankovA given
404fe2e029eSRobert Mustacchi.Nm
40572d3dbb9SYuri Pankovfile supports up to 32767 (0x7fff) types.
40672d3dbb9SYuri PankovThe first valid type identifier is 0x1.
407fe2e029eSRobert MustacchiWhen a given
408fe2e029eSRobert Mustacchi.Nm
409fe2e029eSRobert Mustacchifile is a child, indicated by a non-zero entry for the
410fe2e029eSRobert Mustacchi.Sy header Ns 's
411fe2e029eSRobert Mustacchi.Em cth_parname ,
412fe2e029eSRobert Mustacchithen the first valid type identifier is 0x8000 and the last is 0xffff.
413fe2e029eSRobert MustacchiIn this case, type identifiers 0x1 through 0x7fff are references to the
414fe2e029eSRobert Mustacchiparent.
415fe2e029eSRobert Mustacchi.Lp
416fe2e029eSRobert MustacchiThe type identifier zero is a sentinel value used to indicate that there
417fe2e029eSRobert Mustacchiis no type information available or it is an unknown type.
418fe2e029eSRobert Mustacchi.Lp
419fe2e029eSRobert MustacchiThroughout the file format, the identifier is stored in different sized
420fe2e029eSRobert Mustacchivalues; however, the minimum size to represent a given identifier is a
421fe2e029eSRobert Mustacchi.Sy uint16_t .
422fe2e029eSRobert MustacchiOther consumers of
423fe2e029eSRobert Mustacchi.Nm
424fe2e029eSRobert Mustacchiinformation may use larger or opaque identifiers.
425fe2e029eSRobert Mustacchi.Ss String Identifiers
426fe2e029eSRobert MustacchiString identifiers are always encoded as four byte unsigned integers
42772d3dbb9SYuri Pankovwhich are an offset into a string table.
42872d3dbb9SYuri PankovThe
429fe2e029eSRobert Mustacchi.Nm
430fe2e029eSRobert Mustacchiformat supports two different string tables which have an identifier of
43172d3dbb9SYuri Pankovzero or one.
43272d3dbb9SYuri PankovThis identifier is stored in the high-order bit of the unsigned four byte
43372d3dbb9SYuri Pankovoffset.
43472d3dbb9SYuri PankovTherefore, the maximum supported offset into one of these tables is 0x7ffffffff.
435fe2e029eSRobert Mustacchi.Lp
436fe2e029eSRobert MustacchiTable identifier zero, always refers to the
437fe2e029eSRobert Mustacchi.Sy string
43872d3dbb9SYuri Pankovsection in the CTF file itself.
43972d3dbb9SYuri PankovString table identifier one refers to an external string table which is the ELF
44072d3dbb9SYuri Pankovstring table for the ELF symbol table associated with the
441fe2e029eSRobert Mustacchi.Nm
442fe2e029eSRobert Mustacchicontainer.
443fe2e029eSRobert Mustacchi.Ss Type Encoding
444fe2e029eSRobert MustacchiEvery
445fe2e029eSRobert Mustacchi.Nm
446fe2e029eSRobert Mustacchitype begins with metadata encoded into a
447fe2e029eSRobert Mustacchi.Sy uint16_t .
448fe2e029eSRobert MustacchiThis encoded information tells us three different pieces of information:
449fe2e029eSRobert Mustacchi.Bl -bullet -offset indent -compact
450fe2e029eSRobert Mustacchi.It
451fe2e029eSRobert MustacchiThe kind of the type
452fe2e029eSRobert Mustacchi.It
453fe2e029eSRobert MustacchiWhether this type is a root type or not
454fe2e029eSRobert Mustacchi.It
455fe2e029eSRobert MustacchiThe length of the variable data
456fe2e029eSRobert Mustacchi.El
457fe2e029eSRobert Mustacchi.Lp
458fe2e029eSRobert MustacchiThe 16 bits that make up the encoding are broken down such that you have
459fe2e029eSRobert Mustacchifive bits for the kind, one bit for indicating whether or not it is a
46072d3dbb9SYuri Pankovroot type, and 10 bits for the variable length.
46172d3dbb9SYuri PankovThis is laid out as follows:
462fe2e029eSRobert Mustacchi.Bd -literal -offset indent
463fe2e029eSRobert Mustacchi+--------------------+
464fe2e029eSRobert Mustacchi| kind | root | vlen |
465fe2e029eSRobert Mustacchi+--------------------+
466fe2e029eSRobert Mustacchi15   11   10   9    0
467fe2e029eSRobert Mustacchi.Ed
468fe2e029eSRobert Mustacchi.Lp
46972d3dbb9SYuri PankovThe current version of the file format defines 14 different kinds.
47072d3dbb9SYuri PankovThe interpretation of these different kinds will be discussed in the section
471fe2e029eSRobert Mustacchi.Sx The Type Section .
472fe2e029eSRobert MustacchiIf a kind is encountered that is not listed below, then it is not a valid
473fe2e029eSRobert Mustacchi.Nm
47472d3dbb9SYuri Pankovfile.
47572d3dbb9SYuri PankovThe kinds are defined as follows:
476fe2e029eSRobert Mustacchi.Bd -literal -offset indent
477fe2e029eSRobert Mustacchi#define	CTF_K_UNKNOWN	0
478fe2e029eSRobert Mustacchi#define	CTF_K_INTEGER	1
479fe2e029eSRobert Mustacchi#define	CTF_K_FLOAT	2
480fe2e029eSRobert Mustacchi#define	CTF_K_POINTER	3
481fe2e029eSRobert Mustacchi#define	CTF_K_ARRAY	4
482fe2e029eSRobert Mustacchi#define	CTF_K_FUNCTION	5
483fe2e029eSRobert Mustacchi#define	CTF_K_STRUCT	6
484fe2e029eSRobert Mustacchi#define	CTF_K_UNION	7
485fe2e029eSRobert Mustacchi#define	CTF_K_ENUM	8
486fe2e029eSRobert Mustacchi#define	CTF_K_FORWARD	9
487fe2e029eSRobert Mustacchi#define	CTF_K_TYPEDEF	10
488fe2e029eSRobert Mustacchi#define	CTF_K_VOLATILE	11
489fe2e029eSRobert Mustacchi#define	CTF_K_CONST	12
490fe2e029eSRobert Mustacchi#define	CTF_K_RESTRICT	13
491fe2e029eSRobert Mustacchi.Ed
492fe2e029eSRobert Mustacchi.Lp
493fe2e029eSRobert MustacchiPrograms directly reference many types; however, other types are referenced
49472d3dbb9SYuri Pankovindirectly because they are part of some other structure.
49572d3dbb9SYuri PankovThese types that are referenced directly and used are called
496fe2e029eSRobert Mustacchi.Sy root
49772d3dbb9SYuri Pankovtypes.
49872d3dbb9SYuri PankovOther types may be used indirectly, for example, a program may reference
49972d3dbb9SYuri Pankova structure directly, but not one of its members which has a type.
50072d3dbb9SYuri PankovThat type is not considered a
501fe2e029eSRobert Mustacchi.Sy root
50272d3dbb9SYuri Pankovtype.
50372d3dbb9SYuri PankovIf a type is a
504fe2e029eSRobert Mustacchi.Sy root
505fe2e029eSRobert Mustacchitype, then it will have bit 10 set.
506fe2e029eSRobert Mustacchi.Lp
507fe2e029eSRobert MustacchiThe variable length section is specific to each kind and is discussed in the
508fe2e029eSRobert Mustacchisection
509fe2e029eSRobert Mustacchi.Sx The Type Section .
510fe2e029eSRobert Mustacchi.Lp
511fe2e029eSRobert MustacchiThe following macros are useful for constructing and deconstructing the encoded
512fe2e029eSRobert Mustacchitype information:
513fe2e029eSRobert Mustacchi.Bd -literal -offset indent
514fe2e029eSRobert Mustacchi
515fe2e029eSRobert Mustacchi#define	CTF_MAX_VLEN	0x3ff
516fe2e029eSRobert Mustacchi#define	CTF_INFO_KIND(info)	(((info) & 0xf800) >> 11)
517fe2e029eSRobert Mustacchi#define	CTF_INFO_ISROOT(info)	(((info) & 0x0400) >> 10)
518fe2e029eSRobert Mustacchi#define	CTF_INFO_VLEN(info)	(((info) & CTF_MAX_VLEN))
519fe2e029eSRobert Mustacchi
520*11994f6fSRobert Mustacchi#define	CTF_TYPE_INFO(kind, isroot, vlen) \e
521fe2e029eSRobert Mustacchi	(((kind) << 11) | (((isroot) ? 1 : 0) << 10) | ((vlen) & CTF_MAX_VLEN))
522fe2e029eSRobert Mustacchi.Ed
523fe2e029eSRobert Mustacchi.Ss The Label Section
524fe2e029eSRobert MustacchiWhen consuming
525fe2e029eSRobert Mustacchi.Nm
526fe2e029eSRobert Mustacchidata, it is often useful to know whether two different
527fe2e029eSRobert Mustacchi.Nm
52872d3dbb9SYuri Pankovcontainers come from the same source base and version.
52972d3dbb9SYuri PankovFor example, when building illumos, there are many kernel modules that are built
53072d3dbb9SYuri Pankovagainst a single collection of source code.
53172d3dbb9SYuri PankovA label is encoded into the
532fe2e029eSRobert Mustacchi.Nm
53372d3dbb9SYuri Pankovfiles that corresponds with the particular build.
53472d3dbb9SYuri PankovThis ensures that if files on the system were to become mixed up from multiple
53572d3dbb9SYuri Pankovreleases, that they are not used together by tools, particularly when a child
53672d3dbb9SYuri Pankovneeds to refer to a type in the parent.
53772d3dbb9SYuri PankovBecause they are linked used the type identifiers, if the wrong parent is used
53872d3dbb9SYuri Pankovthen the wrong type will be encountered.
539fe2e029eSRobert Mustacchi.Lp
540fe2e029eSRobert MustacchiEach label is encoded in the file format using the following eight byte
541fe2e029eSRobert Mustacchistructure:
542fe2e029eSRobert Mustacchi.Bd -literal
543fe2e029eSRobert Mustacchitypedef struct ctf_lblent {
544fe2e029eSRobert Mustacchi	uint_t ctl_label;	/* ref to name of label */
545fe2e029eSRobert Mustacchi	uint_t ctl_typeidx;	/* last type associated with this label */
546fe2e029eSRobert Mustacchi} ctf_lblent_t;
547fe2e029eSRobert Mustacchi.Ed
548fe2e029eSRobert Mustacchi.Lp
549fe2e029eSRobert MustacchiEach label has two different components, a name and a type identifier.
550fe2e029eSRobert MustacchiThe name is encoded in the
551fe2e029eSRobert Mustacchi.Em ctl_label
552fe2e029eSRobert Mustacchimember which is in the format defined in the section
553fe2e029eSRobert Mustacchi.Sx String Identifiers .
554fe2e029eSRobert MustacchiGenerally, the names of all labels are found in the internal string
555fe2e029eSRobert Mustacchisection.
556fe2e029eSRobert Mustacchi.Lp
557fe2e029eSRobert MustacchiThe type identifier encoded in the member
558fe2e029eSRobert Mustacchi.Em ctl_typeidx
559fe2e029eSRobert Mustacchirefers to the last type identifier that a label refers to in the current
56072d3dbb9SYuri Pankovfile.
56172d3dbb9SYuri PankovLabels only refer to types in the current file, if the
562fe2e029eSRobert Mustacchi.Nm
563fe2e029eSRobert Mustacchifile is a child, then it will have the same label as its parent;
564fe2e029eSRobert Mustacchihowever, its label will only refer to its types, not its parents.
565fe2e029eSRobert Mustacchi.Lp
566fe2e029eSRobert MustacchiIt is also possible, though rather uncommon, for a
567fe2e029eSRobert Mustacchi.Nm
56872d3dbb9SYuri Pankovfile to have multiple labels.
56972d3dbb9SYuri PankovLabels are placed one after another, every eight bytes.
57072d3dbb9SYuri PankovWhen multiple labels are present, types may only belong to a single label.
571fe2e029eSRobert Mustacchi.Ss The Object Section
572fe2e029eSRobert MustacchiThe object section provides a mapping from ELF symbols of type
573fe2e029eSRobert Mustacchi.Sy STT_OBJECT
57472d3dbb9SYuri Pankovin the symbol table to a type identifier.
57572d3dbb9SYuri PankovEvery entry in this section is a
576fe2e029eSRobert Mustacchi.Sy uint16_t
577fe2e029eSRobert Mustacchiwhich contains a type identifier as described in the section
578fe2e029eSRobert Mustacchi.Sx Type Identifiers .
579fe2e029eSRobert MustacchiIf there is no information for an object, then the type identifier 0x0
580fe2e029eSRobert Mustacchiis stored for that entry.
581fe2e029eSRobert Mustacchi.Lp
582fe2e029eSRobert MustacchiTo walk the object section, you need to have a corresponding
583fe2e029eSRobert Mustacchi.Sy symbol table
584fe2e029eSRobert Mustacchiin the ELF object that contains the
585fe2e029eSRobert Mustacchi.Nm
58672d3dbb9SYuri Pankovdata.
58772d3dbb9SYuri PankovNot every object is included in this section.
58872d3dbb9SYuri PankovSpecifically, when walking the symbol table.
58972d3dbb9SYuri PankovAn entry is skipped if it matches any of the following conditions:
590fe2e029eSRobert Mustacchi.Lp
591fe2e029eSRobert Mustacchi.Bl -bullet -offset indent -compact
592fe2e029eSRobert Mustacchi.It
593fe2e029eSRobert MustacchiThe symbol type is not
594fe2e029eSRobert Mustacchi.Sy STT_OBJECT
595fe2e029eSRobert Mustacchi.It
596fe2e029eSRobert MustacchiThe symbol's section index is
597fe2e029eSRobert Mustacchi.Sy SHN_UNDEF
598fe2e029eSRobert Mustacchi.It
599fe2e029eSRobert MustacchiThe symbol's name offset is zero
600fe2e029eSRobert Mustacchi.It
601fe2e029eSRobert MustacchiThe symbol's section index is
602fe2e029eSRobert Mustacchi.Sy SHN_ABS
603fe2e029eSRobert Mustacchiand the value of the symbol is zero.
604fe2e029eSRobert Mustacchi.It
605fe2e029eSRobert MustacchiThe symbol's name is
606fe2e029eSRobert Mustacchi.Li _START_
607fe2e029eSRobert Mustacchior
608fe2e029eSRobert Mustacchi.Li _END_ .
609fe2e029eSRobert MustacchiThese are skipped because they are used for scoping local symbols in
610fe2e029eSRobert MustacchiELF.
611fe2e029eSRobert Mustacchi.El
612fe2e029eSRobert Mustacchi.Lp
613fe2e029eSRobert MustacchiThe following sample code shows an example of iterating the object
614fe2e029eSRobert Mustacchisection and skipping the correct symbols:
615fe2e029eSRobert Mustacchi.Bd -literal
616fe2e029eSRobert Mustacchi#include <gelf.h>
617fe2e029eSRobert Mustacchi#include <stdio.h>
618fe2e029eSRobert Mustacchi
619fe2e029eSRobert Mustacchi/*
620fe2e029eSRobert Mustacchi * Given the start of the object section in the CTF file, the number of symbols,
621fe2e029eSRobert Mustacchi * and the ELF Data sections for the symbol table and the string table, this
622fe2e029eSRobert Mustacchi * prints the type identifiers that correspond to objects. Note, a more robust
623fe2e029eSRobert Mustacchi * implementation should ensure that they don't walk beyond the end of the CTF
624fe2e029eSRobert Mustacchi * object section.
625fe2e029eSRobert Mustacchi */
626fe2e029eSRobert Mustacchistatic int
627fe2e029eSRobert Mustacchiwalk_symbols(uint16_t *objtoff, Elf_Data *symdata, Elf_Data *strdata,
628fe2e029eSRobert Mustacchi    long nsyms)
629fe2e029eSRobert Mustacchi{
630fe2e029eSRobert Mustacchi	long i;
631fe2e029eSRobert Mustacchi	uintptr_t strbase = strdata->d_buf;
632fe2e029eSRobert Mustacchi
633fe2e029eSRobert Mustacchi	for (i = 1; i < nsyms; i++, objftoff++) {
634fe2e029eSRobert Mustacchi		const char *name;
635fe2e029eSRobert Mustacchi		GElf_Sym sym;
636fe2e029eSRobert Mustacchi
637fe2e029eSRobert Mustacchi		if (gelf_getsym(symdata, i, &sym) == NULL)
638fe2e029eSRobert Mustacchi			return (1);
639fe2e029eSRobert Mustacchi
640fe2e029eSRobert Mustacchi		if (GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
641fe2e029eSRobert Mustacchi			continue;
642fe2e029eSRobert Mustacchi		if (sym.st_shndx == SHN_UNDEF || sym.st_name == 0)
643fe2e029eSRobert Mustacchi			continue;
644fe2e029eSRobert Mustacchi		if (sym.st_shndx == SHN_ABS && sym.st_value == 0)
645fe2e029eSRobert Mustacchi			continue;
646fe2e029eSRobert Mustacchi		name = (const char *)(strbase + sym.st_name);
647fe2e029eSRobert Mustacchi		if (strcmp(name, "_START_") == 0 || strcmp(name, "_END_") == 0)
648fe2e029eSRobert Mustacchi			continue;
649fe2e029eSRobert Mustacchi
650fe2e029eSRobert Mustacchi		(void) printf("Symbol %d has type %d\n", i, *objtoff);
651fe2e029eSRobert Mustacchi	}
652fe2e029eSRobert Mustacchi
653fe2e029eSRobert Mustacchi	return (0);
654fe2e029eSRobert Mustacchi}
655fe2e029eSRobert Mustacchi.Ed
656fe2e029eSRobert Mustacchi.Ss The Function Section
657fe2e029eSRobert MustacchiThe function section of the
658fe2e029eSRobert Mustacchi.Nm
659fe2e029eSRobert Mustacchifile encodes the types of both the function's arguments and the function's
66072d3dbb9SYuri Pankovreturn type.
66172d3dbb9SYuri PankovSimilar to
662fe2e029eSRobert Mustacchi.Sx The Object Section ,
663fe2e029eSRobert Mustacchithe function section encodes information for all symbols of type
664fe2e029eSRobert Mustacchi.Sy STT_FUNCTION ,
66572d3dbb9SYuri Pankovexcepting those that fit specific criteria.
66672d3dbb9SYuri PankovUnlike with objects, because functions have a variable number of arguments, they
66772d3dbb9SYuri Pankovstart with a type encoding as defined in
668fe2e029eSRobert Mustacchi.Sx Type Encoding ,
669fe2e029eSRobert Mustacchiwhich is the size of a
670fe2e029eSRobert Mustacchi.Sy uint16_t .
671fe2e029eSRobert MustacchiFor functions which have no type information available, they are encoded as
672fe2e029eSRobert Mustacchi.Li CTF_TYPE_INFO(CTF_K_UNKNOWN, 0, 0) .
67372d3dbb9SYuri PankovFunctions with arguments are encoded differently.
67472d3dbb9SYuri PankovHere, the variable length is turned into the number of arguments in the
67572d3dbb9SYuri Pankovfunction.
67672d3dbb9SYuri PankovIf a function is a
677fe2e029eSRobert Mustacchi.Sy varargs
67872d3dbb9SYuri Pankovtype function, then the number of arguments is increased by one.
67972d3dbb9SYuri PankovFunctions with type information are encoded as:
680fe2e029eSRobert Mustacchi.Li CTF_TYPE_INFO(CTF_K_FUNCTION, 0, nargs) .
681fe2e029eSRobert Mustacchi.Lp
682fe2e029eSRobert MustacchiFor functions that have no type information, nothing else is encoded, and the
68372d3dbb9SYuri Pankovnext function is encoded.
68472d3dbb9SYuri PankovFor functions with type information, the next
685fe2e029eSRobert Mustacchi.Sy uint16_t
68672d3dbb9SYuri Pankovis encoded with the type identifier of the return type of the function.
68772d3dbb9SYuri PankovIt is followed by each of the type identifiers of the arguments, if any exist,
68872d3dbb9SYuri Pankovin the order that they appear in the function.
68972d3dbb9SYuri PankovTherefore, argument 0 is the first type identifier and so on.
69072d3dbb9SYuri PankovWhen a function has a final varargs argument, that is encoded with the type
69172d3dbb9SYuri Pankovidentifier of zero.
692fe2e029eSRobert Mustacchi.Lp
693fe2e029eSRobert MustacchiLike
694fe2e029eSRobert Mustacchi.Sx The Object Section ,
69572d3dbb9SYuri Pankovthe function section is encoded in the order of the symbol table.
69672d3dbb9SYuri PankovIt has similar, but slightly different considerations from objects.
69772d3dbb9SYuri PankovWhile iterating the symbol table, if any of the following conditions are true,
69872d3dbb9SYuri Pankovthen the entry is skipped and no corresponding entry is written:
699fe2e029eSRobert Mustacchi.Lp
700fe2e029eSRobert Mustacchi.Bl -bullet -offset indent -compact
701fe2e029eSRobert Mustacchi.It
702fe2e029eSRobert MustacchiThe symbol type is not
703fe2e029eSRobert Mustacchi.Sy STT_FUNCTION
704fe2e029eSRobert Mustacchi.It
705fe2e029eSRobert MustacchiThe symbol's section index is
706fe2e029eSRobert Mustacchi.Sy SHN_UNDEF
707fe2e029eSRobert Mustacchi.It
708fe2e029eSRobert MustacchiThe symbol's name offset is zero
709fe2e029eSRobert Mustacchi.It
710fe2e029eSRobert MustacchiThe symbol's name is
711fe2e029eSRobert Mustacchi.Li _START_
712fe2e029eSRobert Mustacchior
713fe2e029eSRobert Mustacchi.Li _END_ .
714fe2e029eSRobert MustacchiThese are skipped because they are used for scoping local symbols in
715fe2e029eSRobert MustacchiELF.
716fe2e029eSRobert Mustacchi.El
717fe2e029eSRobert Mustacchi.Ss The Type Section
718fe2e029eSRobert MustacchiThe type section is the heart of the
719fe2e029eSRobert Mustacchi.Nm
72072d3dbb9SYuri Pankovdata.
72172d3dbb9SYuri PankovIt encodes all of the information about the types themselves.
72272d3dbb9SYuri PankovThe base of the type information comes in two forms, a short form and a long
72372d3dbb9SYuri Pankovform, each of which may be followed by a variable number of arguments.
72472d3dbb9SYuri PankovThe following definitions describe the short and long forms:
725fe2e029eSRobert Mustacchi.Bd -literal
726fe2e029eSRobert Mustacchi#define	CTF_MAX_SIZE	0xfffe	/* max size of a type in bytes */
727fe2e029eSRobert Mustacchi#define	CTF_LSIZE_SENT	0xffff	/* sentinel for ctt_size */
728fe2e029eSRobert Mustacchi#define	CTF_MAX_LSIZE	UINT64_MAX
729fe2e029eSRobert Mustacchi
730fe2e029eSRobert Mustacchitypedef struct ctf_stype {
731fe2e029eSRobert Mustacchi	uint_t ctt_name;	/* reference to name in string table */
732fe2e029eSRobert Mustacchi	ushort_t ctt_info;	/* encoded kind, variant length */
733fe2e029eSRobert Mustacchi	union {
734fe2e029eSRobert Mustacchi		ushort_t _size;	/* size of entire type in bytes */
735fe2e029eSRobert Mustacchi		ushort_t _type;	/* reference to another type */
736fe2e029eSRobert Mustacchi	} _u;
737fe2e029eSRobert Mustacchi} ctf_stype_t;
738fe2e029eSRobert Mustacchi
739fe2e029eSRobert Mustacchitypedef struct ctf_type {
740fe2e029eSRobert Mustacchi	uint_t ctt_name;	/* reference to name in string table */
741fe2e029eSRobert Mustacchi	ushort_t ctt_info;	/* encoded kind, variant length */
742fe2e029eSRobert Mustacchi	union {
743fe2e029eSRobert Mustacchi		ushort_t _size;	/* always CTF_LSIZE_SENT */
744fe2e029eSRobert Mustacchi		ushort_t _type; /* do not use */
745fe2e029eSRobert Mustacchi	} _u;
746fe2e029eSRobert Mustacchi	uint_t ctt_lsizehi;	/* high 32 bits of type size in bytes */
747fe2e029eSRobert Mustacchi	uint_t ctt_lsizelo;	/* low 32 bits of type size in bytes */
748fe2e029eSRobert Mustacchi} ctf_type_t;
749fe2e029eSRobert Mustacchi
750fe2e029eSRobert Mustacchi#define	ctt_size _u._size	/* for fundamental types that have a size */
751fe2e029eSRobert Mustacchi#define	ctt_type _u._type	/* for types that reference another type */
752fe2e029eSRobert Mustacchi.Ed
753fe2e029eSRobert Mustacchi.Pp
754fe2e029eSRobert MustacchiType sizes are stored in
755fe2e029eSRobert Mustacchi.Sy bytes .
756fe2e029eSRobert MustacchiThe basic small form uses a
757fe2e029eSRobert Mustacchi.Sy ushort_t
75872d3dbb9SYuri Pankovto store the number of bytes.
75972d3dbb9SYuri PankovIf the number of bytes in a structure would exceed 0xfffe, then the alternate
76072d3dbb9SYuri Pankovform, the
761fe2e029eSRobert Mustacchi.Sy ctf_type_t ,
76272d3dbb9SYuri Pankovis used instead.
76372d3dbb9SYuri PankovTo indicate that the larger form is being used, the member
764fe2e029eSRobert Mustacchi.Em ctt_size
765fe2e029eSRobert Mustacchiis set to value of
766fe2e029eSRobert Mustacchi.Sy CTF_LSIZE_SENT
76772d3dbb9SYuri Pankov(0xffff).
76872d3dbb9SYuri PankovIn general, when going through the type section, consumers use the
769fe2e029eSRobert Mustacchi.Sy ctf_type_t
770fe2e029eSRobert Mustacchistructure, but pay attention to the value of the member
771fe2e029eSRobert Mustacchi.Em ctt_size
772fe2e029eSRobert Mustacchito determine whether they should increment their scan by the size of the
773fe2e029eSRobert Mustacchi.Sy ctf_stype_t
774fe2e029eSRobert Mustacchior
775fe2e029eSRobert Mustacchi.Sy ctf_type_t .
776fe2e029eSRobert MustacchiNot all kinds of types use
777fe2e029eSRobert Mustacchi.Sy ctt_size .
778fe2e029eSRobert MustacchiThose which do not, will always use the
779fe2e029eSRobert Mustacchi.Sy ctf_stype_t
78072d3dbb9SYuri Pankovstructure.
78172d3dbb9SYuri PankovThe individual sections for each kind have more information.
782fe2e029eSRobert Mustacchi.Lp
78372d3dbb9SYuri PankovTypes are written out in order.
78472d3dbb9SYuri PankovTherefore the first entry encountered has a type id of 0x1, or 0x8000 if a
78572d3dbb9SYuri Pankovchild.
78672d3dbb9SYuri PankovThe member
787fe2e029eSRobert Mustacchi.Em ctt_name
788fe2e029eSRobert Mustacchiis encoded as described in the section
789fe2e029eSRobert Mustacchi.Sx String Identifiers .
79072d3dbb9SYuri PankovThe string that it points to is the name of the type.
79172d3dbb9SYuri PankovIf the identifier points to an empty string (one that consists solely of a null
79272d3dbb9SYuri Pankovterminator) then the type does not have a name, this is common with anonymous
79372d3dbb9SYuri Pankovstructures and unions that only have a typedef to name them, as well as,
79472d3dbb9SYuri Pankovpointers and qualifiers.
795fe2e029eSRobert Mustacchi.Lp
796fe2e029eSRobert MustacchiThe next member, the
797fe2e029eSRobert Mustacchi.Em ctt_info ,
798fe2e029eSRobert Mustacchiis encoded as described in the section
799fe2e029eSRobert Mustacchi.Sx Type Encoding .
800fe2e029eSRobert MustacchiThe types kind tells us how to interpret the remaining data in the
801fe2e029eSRobert Mustacchi.Sy ctf_type_t
80272d3dbb9SYuri Pankovand any variable length data that may exist.
80372d3dbb9SYuri PankovThe rest of this section will be broken down into the interpretation of the
80472d3dbb9SYuri Pankovvarious kinds.
805fe2e029eSRobert Mustacchi.Ss Encoding of Integers
806fe2e029eSRobert MustacchiIntegers, which are of type
807fe2e029eSRobert Mustacchi.Sy CTF_K_INTEGER ,
80872d3dbb9SYuri Pankovhave no variable length arguments.
80972d3dbb9SYuri PankovInstead, they are followed by a four byte
810fe2e029eSRobert Mustacchi.Sy uint_t
81172d3dbb9SYuri Pankovwhich describes their encoding.
81272d3dbb9SYuri PankovAll integers must be encoded with a variable length of zero.
81372d3dbb9SYuri PankovThe
814fe2e029eSRobert Mustacchi.Em ctt_size
81572d3dbb9SYuri Pankovmember describes the length of the integer in bytes.
81672d3dbb9SYuri PankovIn general, integer sizes will be rounded up to the closest power of two.
817fe2e029eSRobert Mustacchi.Lp
818fe2e029eSRobert MustacchiThe integer encoding contains three different pieces of information:
819fe2e029eSRobert Mustacchi.Bl -bullet -offset indent -compact
820fe2e029eSRobert Mustacchi.It
821fe2e029eSRobert MustacchiThe encoding of the integer
822fe2e029eSRobert Mustacchi.It
823fe2e029eSRobert MustacchiThe offset in
824fe2e029eSRobert Mustacchi.Sy bits
825fe2e029eSRobert Mustacchiof the type
826fe2e029eSRobert Mustacchi.It
827fe2e029eSRobert MustacchiThe size in
828fe2e029eSRobert Mustacchi.Sy bits
829fe2e029eSRobert Mustacchiof the type
830fe2e029eSRobert Mustacchi.El
831fe2e029eSRobert Mustacchi.Pp
832fe2e029eSRobert MustacchiThis encoding can be expressed through the following macros:
833fe2e029eSRobert Mustacchi.Bd -literal -offset indent
834fe2e029eSRobert Mustacchi#define	CTF_INT_ENCODING(data)	(((data) & 0xff000000) >> 24)
835fe2e029eSRobert Mustacchi#define	CTF_INT_OFFSET(data)	(((data) & 0x00ff0000) >> 16)
836fe2e029eSRobert Mustacchi#define	CTF_INT_BITS(data)	(((data) & 0x0000ffff))
837fe2e029eSRobert Mustacchi
838*11994f6fSRobert Mustacchi#define	CTF_INT_DATA(encoding, offset, bits) \e
839fe2e029eSRobert Mustacchi	(((encoding) << 24) | ((offset) << 16) | (bits))
840fe2e029eSRobert Mustacchi.Ed
841fe2e029eSRobert Mustacchi.Pp
842fe2e029eSRobert MustacchiThe following flags are defined for the encoding at this time:
843fe2e029eSRobert Mustacchi.Bd -literal -offset indent
844fe2e029eSRobert Mustacchi#define	CTF_INT_SIGNED		0x01
845fe2e029eSRobert Mustacchi#define	CTF_INT_CHAR		0x02
846fe2e029eSRobert Mustacchi#define	CTF_INT_BOOL		0x04
847fe2e029eSRobert Mustacchi#define	CTF_INT_VARARGS		0x08
848fe2e029eSRobert Mustacchi.Ed
849fe2e029eSRobert Mustacchi.Lp
850fe2e029eSRobert MustacchiBy default, an integer is considered to be unsigned, unless it has the
851fe2e029eSRobert Mustacchi.Sy CTF_INT_SIGNED
85272d3dbb9SYuri Pankovflag set.
85372d3dbb9SYuri PankovIf the flag
854fe2e029eSRobert Mustacchi.Sy CTF_INT_CHAR
855fe2e029eSRobert Mustacchiis set, that indicates that the integer is of a type that stores character
856fe2e029eSRobert Mustacchidata, for example the intrinsic C type
857fe2e029eSRobert Mustacchi.Sy char
858fe2e029eSRobert Mustacchiwould have the
859fe2e029eSRobert Mustacchi.Sy CTF_INT_CHAR
86072d3dbb9SYuri Pankovflag set.
86172d3dbb9SYuri PankovIf the flag
862fe2e029eSRobert Mustacchi.Sy CTF_INT_BOOL
86372d3dbb9SYuri Pankovis set, that indicates that the integer represents a boolean type.
86472d3dbb9SYuri PankovFor example, the intrinsic C type
865fe2e029eSRobert Mustacchi.Sy _Bool
866fe2e029eSRobert Mustacchiwould have the
867fe2e029eSRobert Mustacchi.Sy CTF_INT_BOOL
86872d3dbb9SYuri Pankovflag set.
86972d3dbb9SYuri PankovFinally, the flag
870fe2e029eSRobert Mustacchi.Sy CTF_INT_VARARGS
871fe2e029eSRobert Mustacchiindicates that the integer is used as part of a variable number of arguments.
872fe2e029eSRobert MustacchiThis encoding is rather uncommon.
873fe2e029eSRobert Mustacchi.Ss Encoding of Floats
874fe2e029eSRobert MustacchiFloats, which are of type
875fe2e029eSRobert Mustacchi.Sy CTF_K_FLOAT ,
87672d3dbb9SYuri Pankovare similar to their integer counterparts.
87772d3dbb9SYuri PankovThey have no variable length arguments and are followed by a four byte encoding
87872d3dbb9SYuri Pankovwhich describes the kind of float that exists.
87972d3dbb9SYuri PankovThe
880fe2e029eSRobert Mustacchi.Em ctt_size
88172d3dbb9SYuri Pankovmember is the size, in bytes, of the float.
88272d3dbb9SYuri PankovThe float encoding has three different pieces of information inside of it:
883fe2e029eSRobert Mustacchi.Lp
884fe2e029eSRobert Mustacchi.Bl -bullet -offset indent -compact
885fe2e029eSRobert Mustacchi.It
886fe2e029eSRobert MustacchiThe specific kind of float that exists
887fe2e029eSRobert Mustacchi.It
888fe2e029eSRobert MustacchiThe offset in
889fe2e029eSRobert Mustacchi.Sy bits
890fe2e029eSRobert Mustacchiof the float
891fe2e029eSRobert Mustacchi.It
892fe2e029eSRobert MustacchiThe size in
893fe2e029eSRobert Mustacchi.Sy bits
894fe2e029eSRobert Mustacchiof the float
895fe2e029eSRobert Mustacchi.El
896fe2e029eSRobert Mustacchi.Lp
897fe2e029eSRobert MustacchiThis encoding can be expressed through the following macros:
898fe2e029eSRobert Mustacchi.Bd -literal -offset indent
899fe2e029eSRobert Mustacchi#define	CTF_FP_ENCODING(data)	(((data) & 0xff000000) >> 24)
900fe2e029eSRobert Mustacchi#define	CTF_FP_OFFSET(data)	(((data) & 0x00ff0000) >> 16)
901fe2e029eSRobert Mustacchi#define	CTF_FP_BITS(data)	(((data) & 0x0000ffff))
902fe2e029eSRobert Mustacchi
903*11994f6fSRobert Mustacchi#define	CTF_FP_DATA(encoding, offset, bits) \e
904fe2e029eSRobert Mustacchi	(((encoding) << 24) | ((offset) << 16) | (bits))
905fe2e029eSRobert Mustacchi.Ed
906fe2e029eSRobert Mustacchi.Lp
907fe2e029eSRobert MustacchiWhere as the encoding for integers was a series of flags, the encoding for
90872d3dbb9SYuri Pankovfloats maps to a specific kind of float.
90972d3dbb9SYuri PankovIt is not a flag-based value.
91072d3dbb9SYuri PankovThe kinds of floats correspond to both their size, and the encoding.
91172d3dbb9SYuri PankovThis covers all of the basic C intrinsic floating point types.
91272d3dbb9SYuri PankovThe following are the different kinds of floats represented in the encoding:
913fe2e029eSRobert Mustacchi.Bd -literal -offset indent
914fe2e029eSRobert Mustacchi#define	CTF_FP_SINGLE	1	/* IEEE 32-bit float encoding */
915fe2e029eSRobert Mustacchi#define	CTF_FP_DOUBLE	2	/* IEEE 64-bit float encoding */
916fe2e029eSRobert Mustacchi#define	CTF_FP_CPLX	3	/* Complex encoding */
917fe2e029eSRobert Mustacchi#define	CTF_FP_DCPLX	4	/* Double complex encoding */
918fe2e029eSRobert Mustacchi#define	CTF_FP_LDCPLX	5	/* Long double complex encoding */
919fe2e029eSRobert Mustacchi#define	CTF_FP_LDOUBLE	6	/* Long double encoding */
920fe2e029eSRobert Mustacchi#define	CTF_FP_INTRVL	7	/* Interval (2x32-bit) encoding */
921fe2e029eSRobert Mustacchi#define	CTF_FP_DINTRVL	8	/* Double interval (2x64-bit) encoding */
922fe2e029eSRobert Mustacchi#define	CTF_FP_LDINTRVL	9	/* Long double interval (2x128-bit) encoding */
923fe2e029eSRobert Mustacchi#define	CTF_FP_IMAGRY	10	/* Imaginary (32-bit) encoding */
924fe2e029eSRobert Mustacchi#define	CTF_FP_DIMAGRY	11	/* Long imaginary (64-bit) encoding */
925fe2e029eSRobert Mustacchi#define	CTF_FP_LDIMAGRY	12	/* Long double imaginary (128-bit) encoding */
926fe2e029eSRobert Mustacchi.Ed
927fe2e029eSRobert Mustacchi.Ss Encoding of Arrays
928fe2e029eSRobert MustacchiArrays, which are of type
929fe2e029eSRobert Mustacchi.Sy CTF_K_ARRAY ,
93072d3dbb9SYuri Pankovhave no variable length arguments.
93172d3dbb9SYuri PankovThey are followed by a structure which describes the number of elements in the
93272d3dbb9SYuri Pankovarray, the type identifier of the elements in the array, and the type identifier
93372d3dbb9SYuri Pankovof the index of the array.
93472d3dbb9SYuri PankovWith arrays, the
935fe2e029eSRobert Mustacchi.Em ctt_size
93672d3dbb9SYuri Pankovmember is set to zero.
93772d3dbb9SYuri PankovThe structure that follows an array is defined as:
938fe2e029eSRobert Mustacchi.Bd -literal
939fe2e029eSRobert Mustacchitypedef struct ctf_array {
940fe2e029eSRobert Mustacchi	ushort_t cta_contents;	/* reference to type of array contents */
941fe2e029eSRobert Mustacchi	ushort_t cta_index;	/* reference to type of array index */
942fe2e029eSRobert Mustacchi	uint_t cta_nelems;	/* number of elements */
943fe2e029eSRobert Mustacchi} ctf_array_t;
944fe2e029eSRobert Mustacchi.Ed
945fe2e029eSRobert Mustacchi.Lp
946fe2e029eSRobert MustacchiThe
947fe2e029eSRobert Mustacchi.Em cta_contents
948fe2e029eSRobert Mustacchiand
949fe2e029eSRobert Mustacchi.Em cta_index
950fe2e029eSRobert Mustacchimembers of the
951fe2e029eSRobert Mustacchi.Sy ctf_array_t
952fe2e029eSRobert Mustacchiare type identifiers which are encoded as per the section
953fe2e029eSRobert Mustacchi.Sx Type Identifiers .
954fe2e029eSRobert MustacchiThe member
955fe2e029eSRobert Mustacchi.Em cta_nelems
95672d3dbb9SYuri Pankovis a simple four byte unsigned count of the number of elements.
95772d3dbb9SYuri PankovThis count may be zero when encountering C99's flexible array members.
958fe2e029eSRobert Mustacchi.Ss Encoding of Functions
959fe2e029eSRobert MustacchiFunction types, which are of type
960fe2e029eSRobert Mustacchi.Sy CTF_K_FUNCTION ,
96172d3dbb9SYuri Pankovuse the variable length list to be the number of arguments in the function.
96272d3dbb9SYuri PankovWhen the function has a final member which is a varargs, then the argument count
96372d3dbb9SYuri Pankovis incremented by one to account for the variable argument.
96472d3dbb9SYuri PankovHere, the
965fe2e029eSRobert Mustacchi.Em ctt_type
966fe2e029eSRobert Mustacchimember is encoded with the type identifier of the return type of the function.
967fe2e029eSRobert MustacchiNote that the
968fe2e029eSRobert Mustacchi.Em ctt_size
969fe2e029eSRobert Mustacchimember is not used here.
970fe2e029eSRobert Mustacchi.Lp
971fe2e029eSRobert MustacchiThe variable argument list contains the type identifiers for the arguments of
97272d3dbb9SYuri Pankovthe function, if any.
97372d3dbb9SYuri PankovEach one is represented by a
974fe2e029eSRobert Mustacchi.Sy uint16_t
975fe2e029eSRobert Mustacchiand encoded according to the
976fe2e029eSRobert Mustacchi.Sx Type Identifiers
97772d3dbb9SYuri Pankovsection.
97872d3dbb9SYuri PankovIf the function's last argument is of type varargs, then it is also written out,
97972d3dbb9SYuri Pankovbut the type identifier is zero.
98072d3dbb9SYuri PankovThis is included in the count of the function's arguments.
981fe2e029eSRobert Mustacchi.Ss Encoding of Structures and Unions
982fe2e029eSRobert MustacchiStructures and Unions, which are encoded with
983fe2e029eSRobert Mustacchi.Sy CTF_K_STRUCT
984fe2e029eSRobert Mustacchiand
985fe2e029eSRobert Mustacchi.Sy CTF_K_UNION
98672d3dbb9SYuri Pankovrespectively,  are very similar constructs in C.
98772d3dbb9SYuri PankovThe main difference between them is the fact that every member of a structure
98872d3dbb9SYuri Pankovfollows one another, where as in a union, all members share the same memory.
98972d3dbb9SYuri PankovThey are also very similar in terms of their encoding in
990fe2e029eSRobert Mustacchi.Nm .
991fe2e029eSRobert MustacchiThe variable length argument for structures and unions represents the number of
99272d3dbb9SYuri Pankovmembers that they have.
99372d3dbb9SYuri PankovThe value of the member
994fe2e029eSRobert Mustacchi.Em ctt_size
99572d3dbb9SYuri Pankovis the size of the structure and union.
99672d3dbb9SYuri PankovThere are two different structures which are used to encode members in the
99772d3dbb9SYuri Pankovvariable list.
99872d3dbb9SYuri PankovWhen the size of a structure or union is greater than or equal to the large
99972d3dbb9SYuri Pankovmember threshold, 8192, then a different structure is used to encode the member,
100072d3dbb9SYuri Pankovall members are encoded using the same structure.
100172d3dbb9SYuri PankovThe structure for members is as follows:
1002fe2e029eSRobert Mustacchi.Bd -literal
1003fe2e029eSRobert Mustacchitypedef struct ctf_member {
1004fe2e029eSRobert Mustacchi	uint_t ctm_name;	/* reference to name in string table */
1005fe2e029eSRobert Mustacchi	ushort_t ctm_type;	/* reference to type of member */
1006fe2e029eSRobert Mustacchi	ushort_t ctm_offset;	/* offset of this member in bits */
1007fe2e029eSRobert Mustacchi} ctf_member_t;
1008fe2e029eSRobert Mustacchi
1009fe2e029eSRobert Mustacchitypedef struct ctf_lmember {
1010fe2e029eSRobert Mustacchi	uint_t ctlm_name;	/* reference to name in string table */
1011fe2e029eSRobert Mustacchi	ushort_t ctlm_type;	/* reference to type of member */
1012fe2e029eSRobert Mustacchi	ushort_t ctlm_pad;	/* padding */
1013fe2e029eSRobert Mustacchi	uint_t ctlm_offsethi;	/* high 32 bits of member offset in bits */
1014fe2e029eSRobert Mustacchi	uint_t ctlm_offsetlo;	/* low 32 bits of member offset in bits */
1015fe2e029eSRobert Mustacchi} ctf_lmember_t;
1016fe2e029eSRobert Mustacchi.Ed
1017fe2e029eSRobert Mustacchi.Lp
1018fe2e029eSRobert MustacchiBoth the
1019fe2e029eSRobert Mustacchi.Em ctm_name
1020fe2e029eSRobert Mustacchiand
1021fe2e029eSRobert Mustacchi.Em ctlm_name
102272d3dbb9SYuri Pankovrefer to the name of the member.
102372d3dbb9SYuri PankovThe name is encoded as an offset into the string table as described by the
102472d3dbb9SYuri Pankovsection
1025fe2e029eSRobert Mustacchi.Sx String Identifiers .
1026fe2e029eSRobert MustacchiThe members
1027fe2e029eSRobert Mustacchi.Sy ctm_type
1028fe2e029eSRobert Mustacchiand
1029fe2e029eSRobert Mustacchi.Sy ctlm_type
103072d3dbb9SYuri Pankovboth refer to the type of the member.
103172d3dbb9SYuri PankovThey are encoded as per the section
1032fe2e029eSRobert Mustacchi.Sx Type Identifiers .
1033fe2e029eSRobert Mustacchi.Lp
1034fe2e029eSRobert MustacchiThe last piece of information that is present is the offset which describes the
103572d3dbb9SYuri Pankovoffset in memory that the member begins at.
103672d3dbb9SYuri PankovFor unions, this value will always be zero because the start of unions in memory
103772d3dbb9SYuri Pankovis always zero.
103872d3dbb9SYuri PankovFor structures, this is the offset in
1039fe2e029eSRobert Mustacchi.Sy bits
104072d3dbb9SYuri Pankovthat the member begins at.
104172d3dbb9SYuri PankovNote that a compiler may lay out a type with padding.
1042fe2e029eSRobert MustacchiThis means that the difference in offset between two consecutive members may be
104372d3dbb9SYuri Pankovlarger than the size of the member.
104472d3dbb9SYuri PankovWhen the size of the overall structure is strictly less than 8192 bytes, the
104572d3dbb9SYuri Pankovnormal structure,
1046fe2e029eSRobert Mustacchi.Sy ctf_member_t ,
1047fe2e029eSRobert Mustacchiis used and the offset in bits is stored in the member
1048fe2e029eSRobert Mustacchi.Em ctm_offset .
1049fe2e029eSRobert MustacchiHowever, when the size of the structure is greater than or equal to 8192 bytes,
105072d3dbb9SYuri Pankovthen the number of bits is split into two 32-bit quantities.
105172d3dbb9SYuri PankovOne member,
1052fe2e029eSRobert Mustacchi.Em ctlm_offsethi ,
1053fe2e029eSRobert Mustacchirepresents the upper 32 bits of the offset, while the other member,
1054fe2e029eSRobert Mustacchi.Em ctlm_offsetlo ,
105572d3dbb9SYuri Pankovrepresents the lower 32 bits of the offset.
105672d3dbb9SYuri PankovThese can be joined together to get a 64-bit sized offset in bits by shifting
105772d3dbb9SYuri Pankovthe member
1058fe2e029eSRobert Mustacchi.Em ctlm_offsethi
1059fe2e029eSRobert Mustacchito the left by thirty two and then doing a binary or of
1060fe2e029eSRobert Mustacchi.Em ctlm_offsetlo .
1061fe2e029eSRobert Mustacchi.Ss Encoding of Enumerations
1062fe2e029eSRobert MustacchiEnumerations, noted by the type
1063fe2e029eSRobert Mustacchi.Sy CTF_K_ENUM ,
106472d3dbb9SYuri Pankovare similar to structures.
106572d3dbb9SYuri PankovEnumerations use the variable list to note the number of values that the
106672d3dbb9SYuri Pankovenumeration contains, which we'll term enumerators.
106772d3dbb9SYuri PankovIn C, an enumeration is always equivalent to the intrinsic type
1068fe2e029eSRobert Mustacchi.Sy int ,
1069fe2e029eSRobert Mustacchithus the value of the member
1070fe2e029eSRobert Mustacchi.Em ctt_size
1071fe2e029eSRobert Mustacchiis always the size of an integer which is determined based on the current model.
1072fe2e029eSRobert MustacchiFor illumos systems, this will always be 4, as an integer is always defined to
1073fe2e029eSRobert Mustacchibe 4 bytes large in both
1074fe2e029eSRobert Mustacchi.Sy ILP32
1075fe2e029eSRobert Mustacchiand
1076fe2e029eSRobert Mustacchi.Sy LP64 ,
1077fe2e029eSRobert Mustacchiregardless of the architecture.
1078fe2e029eSRobert Mustacchi.Lp
1079fe2e029eSRobert MustacchiThe enumerators encoded in an enumeration have the following structure in the
1080fe2e029eSRobert Mustacchivariable list:
1081fe2e029eSRobert Mustacchi.Bd -literal
1082fe2e029eSRobert Mustacchitypedef struct ctf_enum {
1083fe2e029eSRobert Mustacchi	uint_t cte_name;	/* reference to name in string table */
1084fe2e029eSRobert Mustacchi	int cte_value;		/* value associated with this name */
1085fe2e029eSRobert Mustacchi} ctf_enum_t;
1086fe2e029eSRobert Mustacchi.Ed
1087fe2e029eSRobert Mustacchi.Pp
1088fe2e029eSRobert MustacchiThe member
1089fe2e029eSRobert Mustacchi.Em cte_name
1090fe2e029eSRobert Mustacchirefers to the name of the enumerator's value, it is encoded according to the
1091fe2e029eSRobert Mustacchirules in the section
1092fe2e029eSRobert Mustacchi.Sx String Identifiers .
1093fe2e029eSRobert MustacchiThe member
1094fe2e029eSRobert Mustacchi.Em cte_value
1095fe2e029eSRobert Mustacchicontains the integer value of this enumerator.
1096fe2e029eSRobert Mustacchi.Ss Encoding of Forward References
1097fe2e029eSRobert MustacchiForward references, types of kind
1098fe2e029eSRobert Mustacchi.Sy CTF_K_FORWARD ,
1099fe2e029eSRobert Mustacchiin a
1100fe2e029eSRobert Mustacchi.Nm
110172d3dbb9SYuri Pankovfile refer to types which may not have a definition at all, only a name.
110272d3dbb9SYuri PankovIf the
1103fe2e029eSRobert Mustacchi.Nm
1104fe2e029eSRobert Mustacchifile is a child, then it may be that the forward is resolved to an
1105fe2e029eSRobert Mustacchiactual type in the parent, otherwise the definition may be in another
1106fe2e029eSRobert Mustacchi.Nm
110772d3dbb9SYuri Pankovcontainer or may not be known at all.
110872d3dbb9SYuri PankovThe only member of the
1109fe2e029eSRobert Mustacchi.Sy ctf_type_t
1110fe2e029eSRobert Mustacchithat matters for a forward declaration is the
1111fe2e029eSRobert Mustacchi.Em ctt_name
1112fe2e029eSRobert Mustacchiwhich points to the name of the forward reference in the string table as
111372d3dbb9SYuri Pankovdescribed earlier.
111472d3dbb9SYuri PankovThere is no other information recorded for forward references.
1115fe2e029eSRobert Mustacchi.Ss Encoding of Pointers, Typedefs, Volatile, Const, and Restrict
1116fe2e029eSRobert MustacchiPointers, typedefs, volatile, const, and restrict are all similar in
1117fe2e029eSRobert Mustacchi.Nm .
111872d3dbb9SYuri PankovThey all refer to another type.
111972d3dbb9SYuri PankovIn the case of typedefs, they provide an alternate name, while volatile, const,
112072d3dbb9SYuri Pankovand restrict change how the type is interpreted in the C programming language.
112172d3dbb9SYuri PankovThis covers the
1122fe2e029eSRobert Mustacchi.Nm
1123fe2e029eSRobert Mustacchikinds
1124fe2e029eSRobert Mustacchi.Sy CTF_K_POINTER ,
1125fe2e029eSRobert Mustacchi.Sy CTF_K_TYPEDEF ,
1126fe2e029eSRobert Mustacchi.Sy CTF_K_VOLATILE ,
1127fe2e029eSRobert Mustacchi.Sy CTF_K_RESTRICT ,
1128fe2e029eSRobert Mustacchiand
1129fe2e029eSRobert Mustacchi.Sy CTF_K_CONST .
1130fe2e029eSRobert Mustacchi.Lp
1131fe2e029eSRobert MustacchiThese types have no variable list entries and use the member
1132fe2e029eSRobert Mustacchi.Em ctt_type
1133fe2e029eSRobert Mustacchito refer to the base type that they modify.
1134fe2e029eSRobert Mustacchi.Ss Encoding of Unknown Types
1135fe2e029eSRobert MustacchiTypes with the kind
1136fe2e029eSRobert Mustacchi.Sy CTF_K_UNKNOWN
113772d3dbb9SYuri Pankovare used to indicate gaps in the type identifier space.
113872d3dbb9SYuri PankovThese entries consume an identifier, but do not define anything.
113972d3dbb9SYuri PankovNothing should refer to these gap identifiers.
1140fe2e029eSRobert Mustacchi.Ss Dependencies Between Types
114172d3dbb9SYuri PankovC types can be imagined as a directed, cyclic, graph.
114272d3dbb9SYuri PankovStructures and unions may refer to each other in a way that creates a cyclic
114372d3dbb9SYuri Pankovdependency.
114472d3dbb9SYuri PankovIn cases such as these, the entire type section must be read in and processed.
114572d3dbb9SYuri PankovConsumers must not assume that every type can be laid out in dependency order;
114672d3dbb9SYuri Pankovthey cannot.
1147fe2e029eSRobert Mustacchi.Ss The String Section
1148fe2e029eSRobert MustacchiThe last section of the
1149fe2e029eSRobert Mustacchi.Nm
1150fe2e029eSRobert Mustacchifile is the
1151fe2e029eSRobert Mustacchi.Sy string
115272d3dbb9SYuri Pankovsection.
115372d3dbb9SYuri PankovThis section encodes all of the strings that appear throughout the other
115472d3dbb9SYuri Pankovsections.
115572d3dbb9SYuri PankovIt is laid out as a series of characters followed by a null terminator.
115672d3dbb9SYuri PankovGenerally, all names are written out in ASCII, as most C compilers do not allow
115772d3dbb9SYuri Pankovand characters to appear in identifiers outside of a subset of ASCII.
115872d3dbb9SYuri PankovHowever, any extended characters sets should be written out as a series of UTF-8
115972d3dbb9SYuri Pankovbytes.
1160fe2e029eSRobert Mustacchi.Lp
1161fe2e029eSRobert MustacchiThe first entry in the section, at offset zero, is a single null
116272d3dbb9SYuri Pankovterminator to reference the empty string.
116372d3dbb9SYuri PankovFollowing that, each C string should be written out, including the null
116472d3dbb9SYuri Pankovterminator.
116572d3dbb9SYuri PankovOffsets that refer to something in this section should refer to the first byte
116672d3dbb9SYuri Pankovwhich begins a string.
116772d3dbb9SYuri PankovBeyond the first byte in the section being the null terminator, the order of
116872d3dbb9SYuri Pankovstrings is unimportant.
1169fe2e029eSRobert Mustacchi.Sh Data Encoding and ELF Considerations
1170fe2e029eSRobert Mustacchi.Nm
1171fe2e029eSRobert Mustacchidata is generally included in ELF objects which specify information to
117272d3dbb9SYuri Pankovidentify the architecture and endianness of the file.
117372d3dbb9SYuri PankovA
1174fe2e029eSRobert Mustacchi.Nm
117572d3dbb9SYuri Pankovcontainer inside such an object must match the endianness of the ELF object.
117672d3dbb9SYuri PankovAside from the question of the endian encoding of data, there should be no other
117772d3dbb9SYuri Pankovdifferences between architectures.
117872d3dbb9SYuri PankovWhile many of the types in this document refer to non-fixed size C integral
117972d3dbb9SYuri Pankovtypes, they are equivalent in the models
1180fe2e029eSRobert Mustacchi.Sy ILP32
1181fe2e029eSRobert Mustacchiand
1182fe2e029eSRobert Mustacchi.Sy LP64 .
1183fe2e029eSRobert MustacchiIf any other model is being used with
1184fe2e029eSRobert Mustacchi.Nm
1185fe2e029eSRobert Mustacchidata that has different sizes, then it must not use the model's sizes for
1186fe2e029eSRobert Mustacchithose integral types and instead use the fixed size equivalents based on an
1187fe2e029eSRobert Mustacchi.Sy ILP32
1188fe2e029eSRobert Mustacchienvironment.
1189fe2e029eSRobert Mustacchi.Lp
1190fe2e029eSRobert MustacchiWhen placing a
1191fe2e029eSRobert Mustacchi.Nm
1192fe2e029eSRobert Mustacchicontainer inside of an ELF object, there are certain conventions that are
1193fe2e029eSRobert Mustacchiexpected for the purposes of tooling being able to find the
1194fe2e029eSRobert Mustacchi.Nm
119572d3dbb9SYuri Pankovdata.
119672d3dbb9SYuri PankovIn particular, a given ELF object should only contain a single
1197fe2e029eSRobert Mustacchi.Nm
119872d3dbb9SYuri Pankovsection.
119972d3dbb9SYuri PankovMultiple containers should be merged together into a single one.
1200fe2e029eSRobert Mustacchi.Lp
1201fe2e029eSRobert MustacchiThe
1202fe2e029eSRobert Mustacchi.Nm
120372d3dbb9SYuri Pankovfile should be included in its own ELF section.
120472d3dbb9SYuri PankovThe section's name must be
1205fe2e029eSRobert Mustacchi.Ql .SUNW_ctf .
1206fe2e029eSRobert MustacchiThe type of the section must be
1207fe2e029eSRobert Mustacchi.Sy SHT_PROGBITS .
1208fe2e029eSRobert MustacchiThe section should have a link set to the symbol table and its address
1209fe2e029eSRobert Mustacchialignment must be 4.
1210fe2e029eSRobert Mustacchi.Sh SEE ALSO
1211fe2e029eSRobert Mustacchi.Xr mdb 1 ,
1212fe2e029eSRobert Mustacchi.Xr gelf 3ELF ,
12133a005aadSYuri Pankov.Xr libelf 3LIB ,
1214bbf21555SRichard Lowe.Xr a.out 5 ,
1215bbf21555SRichard Lowe.Xr dtrace 8
1216