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